
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>TypeScript 2.0 · TypeScript Handbook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Patrick Zhong">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="TypeScript 1.8.html" />
    
    
    <link rel="prev" href="TypeScript 2.1.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../handbook/tutorials/">
            
                <a href="../handbook/tutorials/">
            
                    
                    快速上手
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../handbook/tutorials/TypeScript in 5 minutes.html">
            
                <a href="../handbook/tutorials/TypeScript in 5 minutes.html">
            
                    
                    5分钟了解TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../handbook/tutorials/ASP.NET Core.html">
            
                <a href="../handbook/tutorials/ASP.NET Core.html">
            
                    
                    ASP.NET Core
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../handbook/tutorials/ASP.NET 4.html">
            
                <a href="../handbook/tutorials/ASP.NET 4.html">
            
                    
                    ASP.NET 4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../handbook/tutorials/Gulp.html">
            
                <a href="../handbook/tutorials/Gulp.html">
            
                    
                    Gulp
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../handbook/tutorials/Knockout.html">
            
                <a href="../handbook/tutorials/Knockout.html">
            
                    
                    Knockout.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../handbook/tutorials/React & Webpack.html">
            
                <a href="../handbook/tutorials/React & Webpack.html">
            
                    
                    React与webpack
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../handbook/tutorials/React.html">
            
                <a href="../handbook/tutorials/React.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../handbook/tutorials/Angular 2.html">
            
                <a href="../handbook/tutorials/Angular 2.html">
            
                    
                    Angular 2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../handbook/tutorials/Migrating from JavaScript.html">
            
                <a href="../handbook/tutorials/Migrating from JavaScript.html">
            
                    
                    从JavaScript迁移到TypeScript
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../handbook/">
            
                <a href="../handbook/">
            
                    
                    手册
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../handbook/Basic Types.html">
            
                <a href="../handbook/Basic Types.html">
            
                    
                    基础类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../handbook/Variable Declarations.html">
            
                <a href="../handbook/Variable Declarations.html">
            
                    
                    变量声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../handbook/Interfaces.html">
            
                <a href="../handbook/Interfaces.html">
            
                    
                    接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../handbook/Classes.html">
            
                <a href="../handbook/Classes.html">
            
                    
                    类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../handbook/Functions.html">
            
                <a href="../handbook/Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../handbook/Generics.html">
            
                <a href="../handbook/Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../handbook/Enums.html">
            
                <a href="../handbook/Enums.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../handbook/Type Inference.html">
            
                <a href="../handbook/Type Inference.html">
            
                    
                    类型推论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../handbook/Type Compatibility.html">
            
                <a href="../handbook/Type Compatibility.html">
            
                    
                    类型兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../handbook/Advanced Types.html">
            
                <a href="../handbook/Advanced Types.html">
            
                    
                    高级类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../handbook/Utilit Types.md">
            
                <span>
            
                    
                    实用工具类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../handbook/Symbols.html">
            
                <a href="../handbook/Symbols.html">
            
                    
                    Symbols
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../handbook/Iterators and Generators.html">
            
                <a href="../handbook/Iterators and Generators.html">
            
                    
                    Iterators 和 Generators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../handbook/Modules.html">
            
                <a href="../handbook/Modules.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="../handbook/Namespaces.html">
            
                <a href="../handbook/Namespaces.html">
            
                    
                    命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="../handbook/Namespaces and Modules.html">
            
                <a href="../handbook/Namespaces and Modules.html">
            
                    
                    命名空间和模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="../handbook/Module Resolution.html">
            
                <a href="../handbook/Module Resolution.html">
            
                    
                    模块解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="../handbook/Declaration Merging.html">
            
                <a href="../handbook/Declaration Merging.html">
            
                    
                    声明合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="../handbook/Writing Definition Files.html">
            
                <a href="../handbook/Writing Definition Files.html">
            
                    
                    书写.d.ts文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="../handbook/JSX.html">
            
                <a href="../handbook/JSX.html">
            
                    
                    JSX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="../handbook/Decorators.html">
            
                <a href="../handbook/Decorators.html">
            
                    
                    Decorators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="../handbook/Mixins.html">
            
                <a href="../handbook/Mixins.html">
            
                    
                    混入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="../handbook/Triple-Slash Directives.html">
            
                <a href="../handbook/Triple-Slash Directives.html">
            
                    
                    三斜线指令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="../handbook/Type Checking JavaScript Files.html">
            
                <a href="../handbook/Type Checking JavaScript Files.html">
            
                    
                    JavaScript文件里的类型检查
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../handbook/declaration files/Introduction.html">
            
                <a href="../handbook/declaration files/Introduction.html">
            
                    
                    如何书写声明文件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../handbook/declaration files/Library Structures.html">
            
                <a href="../handbook/declaration files/Library Structures.html">
            
                    
                    结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../handbook/declaration files/Do's and Don'ts.html">
            
                <a href="../handbook/declaration files/Do's and Don'ts.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../handbook/declaration files/By Example.html">
            
                <a href="../handbook/declaration files/By Example.html">
            
                    
                    举例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../handbook/declaration files/Deep Dive.html">
            
                <a href="../handbook/declaration files/Deep Dive.html">
            
                    
                    深入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../handbook/declaration files/Publishing.html">
            
                <a href="../handbook/declaration files/Publishing.html">
            
                    
                    发布
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../handbook/declaration files/Consumption.html">
            
                <a href="../handbook/declaration files/Consumption.html">
            
                    
                    使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../handbook/tsconfig.json.html">
            
                <a href="../handbook/tsconfig.json.html">
            
                    
                    工程配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../handbook/tsconfig.json.html">
            
                <a href="../handbook/tsconfig.json.html">
            
                    
                    tsconfig.json
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../handbook/Project References.html">
            
                <a href="../handbook/Project References.html">
            
                    
                    工程引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../handbook/Typings for NPM Packages.html">
            
                <a href="../handbook/Typings for NPM Packages.html">
            
                    
                    NPM包的类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../handbook/Compiler Options.html">
            
                <a href="../handbook/Compiler Options.html">
            
                    
                    编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../handbook/Configuring Watch.html">
            
                <a href="../handbook/Configuring Watch.html">
            
                    
                    配置 Watch
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../handbook/Compiler Options in MSBuild.html">
            
                <a href="../handbook/Compiler Options in MSBuild.html">
            
                    
                    在MSBuild里使用编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../handbook/Integrating with Build Tools.html">
            
                <a href="../handbook/Integrating with Build Tools.html">
            
                    
                    与其它构建工具整合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="../handbook/Nightly Builds.html">
            
                <a href="../handbook/Nightly Builds.html">
            
                    
                    使用TypeScript的每日构建版本
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../wiki/">
            
                <a href="../wiki/">
            
                    
                    Wiki
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../wiki/this-in-TypeScript.html">
            
                <a href="../wiki/this-in-TypeScript.html">
            
                    
                    TypeScript里的this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../wiki/coding_guidelines.html">
            
                <a href="../wiki/coding_guidelines.html">
            
                    
                    编码规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../wiki/Common Errors.html">
            
                <a href="../wiki/Common Errors.html">
            
                    
                    常见编译错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../wiki/TypeScript-Editor-Support.html">
            
                <a href="../wiki/TypeScript-Editor-Support.html">
            
                    
                    支持TypeScript的编辑器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                <a href="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                    
                    结合ASP.NET v5使用TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../wiki/Architectural-Overview.html">
            
                <a href="../wiki/Architectural-Overview.html">
            
                    
                    架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../wiki/Roadmap.html">
            
                <a href="../wiki/Roadmap.html">
            
                    
                    发展路线图
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="./">
            
                <a href="./">
            
                    
                    新增功能
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="TypeScript 3.1.html">
            
                <a href="TypeScript 3.1.html">
            
                    
                    TypeScript 3.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="TypeScript 3.0.html">
            
                <a href="TypeScript 3.0.html">
            
                    
                    TypeScript 3.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="TypeScript 2.9.html">
            
                <a href="TypeScript 2.9.html">
            
                    
                    TypeScript 2.9
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="TypeScript 2.8.html">
            
                <a href="TypeScript 2.8.html">
            
                    
                    TypeScript 2.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="TypeScript 2.7.html">
            
                <a href="TypeScript 2.7.html">
            
                    
                    TypeScript 2.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="TypeScript 2.6.html">
            
                <a href="TypeScript 2.6.html">
            
                    
                    TypeScript 2.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="TypeScript 2.5.html">
            
                <a href="TypeScript 2.5.html">
            
                    
                    TypeScript 2.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="TypeScript 2.4.html">
            
                <a href="TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="TypeScript 2.3.html">
            
                <a href="TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="TypeScript 2.2.html">
            
                <a href="TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.11" data-path="TypeScript 2.1.html">
            
                <a href="TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7.12" data-path="TypeScript 2.0.html">
            
                <a href="TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.13" data-path="TypeScript 1.8.html">
            
                <a href="TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.14" data-path="TypeScript 1.7.html">
            
                <a href="TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.15" data-path="TypeScript 1.6.html">
            
                <a href="TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.16" data-path="TypeScript 1.5.html">
            
                <a href="TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.17" data-path="TypeScript 1.4.html">
            
                <a href="TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.18" data-path="TypeScript 1.3.html">
            
                <a href="TypeScript 1.3.html">
            
                    
                    TypeScript 1.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.19" data-path="TypeScript 1.1.html">
            
                <a href="TypeScript 1.1.html">
            
                    
                    TypeScript 1.1
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../breaking-changes/breaking-changes.html">
            
                <a href="../breaking-changes/breaking-changes.html">
            
                    
                    Breaking Changes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../breaking-changes/TypeScript 2.4.html">
            
                <a href="../breaking-changes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../breaking-changes/TypeScript 2.3.html">
            
                <a href="../breaking-changes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../breaking-changes/TypeScript 2.2.html">
            
                <a href="../breaking-changes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../breaking-changes/TypeScript 2.1.html">
            
                <a href="../breaking-changes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../breaking-changes/TypeScript 2.0.html">
            
                <a href="../breaking-changes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../breaking-changes/TypeScript 1.8.html">
            
                <a href="../breaking-changes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../breaking-changes/TypeScript 1.7.html">
            
                <a href="../breaking-changes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../breaking-changes/TypeScript 1.6.html">
            
                <a href="../breaking-changes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../breaking-changes/TypeScript 1.5.html">
            
                <a href="../breaking-changes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.10" data-path="../breaking-changes/TypeScript 1.4.html">
            
                <a href="../breaking-changes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >TypeScript 2.0</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="typescript-20">TypeScript 2.0</h1>
<h2 id="null&#x548C;undefined&#x7C7B;&#x578B;">Null&#x548C;undefined&#x7C7B;&#x578B;</h2>
<p>TypeScript&#x73B0;&#x5728;&#x6709;&#x4E24;&#x4E2A;&#x7279;&#x6B8A;&#x7684;&#x7C7B;&#x578B;&#xFF1A;Null&#x548C;Undefined, &#x5B83;&#x4EEC;&#x7684;&#x503C;&#x5206;&#x522B;&#x662F;<code>null</code>&#x548C;<code>undefined</code>&#x3002;
&#x4EE5;&#x524D;&#x8FD9;&#x662F;&#x4E0D;&#x53EF;&#x80FD;&#x660E;&#x786E;&#x5730;&#x547D;&#x540D;&#x8FD9;&#x4E9B;&#x7C7B;&#x578B;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x73B0;&#x5728;<code>null</code>&#x548C;<code>undefined</code>&#x4E0D;&#x7BA1;&#x5728;&#x4EC0;&#x4E48;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E0B;&#x90FD;&#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x7C7B;&#x578B;&#x540D;&#x79F0;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x4EE5;&#x524D;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x5668;&#x8BA4;&#x4E3A;<code>null</code>&#x548C;<code>undefined</code>&#x8D4B;&#x503C;&#x7ED9;&#x4E00;&#x5207;&#x3002;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;<code>null</code>&#x548C;<code>undefined</code>&#x662F;&#x6BCF;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x6709;&#x6548;&#x503C;&#xFF0C;
&#x5E76;&#x4E14;&#x4E0D;&#x80FD;&#x660E;&#x786E;&#x6392;&#x9664;&#x5B83;&#x4EEC;&#xFF08;&#x56E0;&#x6B64;&#x4E0D;&#x53EF;&#x80FD;&#x68C0;&#x6D4B;&#x5230;&#x9519;&#x8BEF;&#xFF09;&#x3002;</p>
<h3 id="-strictnullchecks"><code>--strictNullChecks</code></h3>
<p><code>--strictNullChecks</code>&#x53EF;&#x4EE5;&#x5207;&#x6362;&#x5230;&#x65B0;&#x7684;&#x4E25;&#x683C;&#x7A7A;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E2D;&#x3002;</p>
<p>&#x5728;&#x4E25;&#x683C;&#x7A7A;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;<code>null</code>&#x548C;<code>undefined</code>&#x503C;<em>&#x4E0D;&#x518D;</em>&#x5C5E;&#x4E8E;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF0C;&#x4EC5;&#x4EC5;&#x5C5E;&#x4E8E;&#x5B83;&#x4EEC;&#x81EA;&#x5DF1;&#x7C7B;&#x578B;&#x548C;<code>any</code>&#x7C7B;&#x578B;&#x7684;&#x503C;
&#xFF08;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x4F8B;&#x5916;&#xFF0C;<code>undefined</code>&#x4E5F;&#x80FD;&#x8D4B;&#x503C;&#x7ED9;<code>void</code>&#xFF09;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x5C3D;&#x7BA1;&#x5728;&#x5E38;&#x89C4;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E0B;<code>T</code>&#x548C;<code>T | undefined</code>&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x76F8;&#x540C;&#x7684;
&#xFF08;&#x56E0;&#x4E3A;<code>undefined</code>&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x4EFB;&#x4F55;<code>T</code>&#x7684;&#x5B50;&#x7C7B;&#x578B;&#xFF09;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x4E25;&#x683C;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E0B;&#x5B83;&#x4EEC;&#x662F;&#x4E0D;&#x540C;&#x7684;&#xFF0C;
&#x5E76;&#x4E14;&#x4EC5;&#x4EC5;<code>T | undefined</code>&#x5141;&#x8BB8;&#x6709;<code>undefined</code>&#x503C;&#xFF0C;<code>T</code>&#x548C;<code>T | null</code>&#x7684;&#x5173;&#x7CFB;&#x540C;&#x6837;&#x5982;&#x6B64;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-comment">// &#x4F7F;&#x7528;--strictNullChecks&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x7F16;&#x8BD1;&#x7684;</span>
<span class="hljs-keyword">let</span> x: <span class="hljs-built_in">number</span>;
<span class="hljs-keyword">let</span> y: <span class="hljs-built_in">number</span> | <span class="hljs-literal">undefined</span>;
<span class="hljs-keyword">let</span> z: <span class="hljs-built_in">number</span> | <span class="hljs-literal">null</span> | <span class="hljs-literal">undefined</span>;
x = <span class="hljs-number">1</span>;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
y = <span class="hljs-number">1</span>;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
z = <span class="hljs-number">1</span>;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
x = <span class="hljs-literal">undefined</span>;  <span class="hljs-comment">// &#x9519;&#x8BEF;</span>
y = <span class="hljs-literal">undefined</span>;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
z = <span class="hljs-literal">undefined</span>;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
x = <span class="hljs-literal">null</span>;  <span class="hljs-comment">// &#x9519;&#x8BEF;</span>
y = <span class="hljs-literal">null</span>;  <span class="hljs-comment">// &#x9519;&#x8BEF;</span>
z = <span class="hljs-literal">null</span>;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
x = y;  <span class="hljs-comment">// &#x9519;&#x8BEF;</span>
x = z;  <span class="hljs-comment">// &#x9519;&#x8BEF;</span>
y = x;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
y = z;  <span class="hljs-comment">// &#x9519;&#x8BEF;</span>
z = x;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
z = y;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
</code></pre>
<h3 id="&#x4F7F;&#x7528;&#x524D;&#x8D4B;&#x503C;&#x68C0;&#x67E5;">&#x4F7F;&#x7528;&#x524D;&#x8D4B;&#x503C;&#x68C0;&#x67E5;</h3>
<p>&#x5728;&#x4E25;&#x683C;&#x7A7A;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x8981;&#x6C42;&#x672A;&#x5305;&#x542B;<code>undefined</code>&#x7C7B;&#x578B;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x5728;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x5FC5;&#x987B;&#x5148;&#x8D4B;&#x503C;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-comment">// &#x4F7F;&#x7528;--strictNullChecks&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x7F16;&#x8BD1;</span>
<span class="hljs-keyword">let</span> x: <span class="hljs-built_in">number</span>;
<span class="hljs-keyword">let</span> y: <span class="hljs-built_in">number</span> | <span class="hljs-literal">null</span>;
<span class="hljs-keyword">let</span> z: <span class="hljs-built_in">number</span> | <span class="hljs-literal">undefined</span>;
x;  <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;&#x4F7F;&#x7528;&#x524D;&#x672A;&#x8D4B;&#x503C;</span>
y;  <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;&#x4F7F;&#x7528;&#x524D;&#x672A;&#x8D4B;&#x503C;</span>
z;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
x = <span class="hljs-number">1</span>;
y = <span class="hljs-literal">null</span>;
x;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
y;  <span class="hljs-comment">// &#x6B63;&#x786E;</span>
</code></pre>
<p>&#x7F16;&#x8BD1;&#x5668;&#x901A;&#x8FC7;&#x6267;&#x884C;<em>&#x57FA;&#x4E8E;&#x63A7;&#x5236;&#x6D41;&#x7684;&#x7C7B;&#x578B;&#x5206;&#x6790;</em>&#x68C0;&#x67E5;&#x53D8;&#x91CF;&#x660E;&#x786E;&#x88AB;&#x8D4B;&#x8FC7;&#x503C;&#x3002;&#x5728;&#x672C;&#x7BC7;&#x6587;&#x7AE0;&#x540E;&#x9762;&#x4F1A;&#x6709;&#x8FDB;&#x4E00;&#x6B65;&#x7684;&#x7EC6;&#x8282;&#x3002;</p>
<h3 id="&#x53EF;&#x9009;&#x53C2;&#x6570;&#x548C;&#x5C5E;&#x6027;">&#x53EF;&#x9009;&#x53C2;&#x6570;&#x548C;&#x5C5E;&#x6027;</h3>
<p>&#x53EF;&#x9009;&#x53C2;&#x6570;&#x548C;&#x5C5E;&#x6027;&#x4F1A;&#x81EA;&#x52A8;&#x628A;<code>undefined</code>&#x6DFB;&#x52A0;&#x5230;&#x4ED6;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x4E2D;&#xFF0C;&#x5373;&#x4F7F;&#x4ED6;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x660E;&#x786E;&#x4E0D;&#x5305;&#x542B;<code>undefined</code>&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x4E24;&#x4E2A;&#x7C7B;&#x578B;&#x662F;&#x5B8C;&#x5168;&#x76F8;&#x540C;&#x7684;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// &#x4F7F;&#x7528;--strictNullChecks&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x7F16;&#x8BD1;</span>
<span class="hljs-keyword">type</span> T1 = (x?: <span class="hljs-built_in">number</span>) =&gt; <span class="hljs-built_in">string</span>;              <span class="hljs-comment">// x&#x7684;&#x7C7B;&#x578B;&#x662F; number | undefined</span>
<span class="hljs-keyword">type</span> T2 = (x?: <span class="hljs-built_in">number</span> | <span class="hljs-literal">undefined</span>) =&gt; <span class="hljs-built_in">string</span>;  <span class="hljs-comment">// x&#x7684;&#x7C7B;&#x578B;&#x662F; number | undefined</span>
</code></pre>
<h3 id="&#x975E;null&#x548C;&#x975E;undefined&#x7C7B;&#x578B;&#x4FDD;&#x62A4;">&#x975E;null&#x548C;&#x975E;undefined&#x7C7B;&#x578B;&#x4FDD;&#x62A4;</h3>
<p>&#x5982;&#x679C;&#x5BF9;&#x8C61;&#x6216;&#x8005;&#x51FD;&#x6570;&#x7684;&#x7C7B;&#x578B;&#x5305;&#x542B;<code>null</code>&#x548C;<code>undefined</code>&#xFF0C;&#x90A3;&#x4E48;&#x8BBF;&#x95EE;&#x5C5E;&#x6027;&#x6216;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#x5C31;&#x4F1A;&#x4EA7;&#x751F;&#x7F16;&#x8BD1;&#x9519;&#x8BEF;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x5BF9;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x8FDB;&#x884C;&#x4E86;&#x6269;&#x5C55;&#xFF0C;&#x4EE5;&#x652F;&#x6301;&#x5BF9;&#x975E;null&#x548C;&#x975E;undefined&#x7684;&#x68C0;&#x67E5;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-comment">// &#x4F7F;&#x7528;--strictNullChecks&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x7F16;&#x8BD1;</span>
<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">x: <span class="hljs-built_in">number</span></span>): <span class="hljs-title">string</span></span>;
<span class="hljs-keyword">let</span> x: <span class="hljs-built_in">number</span> | <span class="hljs-literal">null</span> | <span class="hljs-literal">undefined</span>;
<span class="hljs-keyword">if</span> (x) {
    f(x);  <span class="hljs-comment">// &#x6B63;&#x786E;&#xFF0C;&#x8FD9;&#x91CC;&#x7684;x&#x7C7B;&#x578B;&#x662F;number</span>
}
<span class="hljs-keyword">else</span> {
    f(x);  <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;&#x8FD9;&#x91CC;&#x7684;x&#x7C7B;&#x578B;&#x662F;number&#xFF1F;</span>
}
<span class="hljs-keyword">let</span> a = x != <span class="hljs-literal">null</span> ? f(x) : <span class="hljs-string">&quot;&quot;</span>;  <span class="hljs-comment">// a&#x7684;&#x7C7B;&#x578B;&#x662F;string</span>
<span class="hljs-keyword">let</span> b = x &amp;&amp; f(x);  <span class="hljs-comment">// b&#x7684;&#x7C7B;&#x578B;&#x662F; string | 0 | null | undefined</span>
</code></pre>
<p>&#x975E;null&#x548C;&#x975E;undefined&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>==</code>&#x3001;<code>!=</code>&#x3001;<code>===</code>&#x6216;<code>!==</code>&#x64CD;&#x4F5C;&#x7B26;&#x548C;<code>null</code>&#x6216;<code>undefined</code>&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#xFF0C;&#x5982;<code>x != null</code>&#x6216;<code>x === undefined</code>&#x3002;&#x5BF9;&#x88AB;&#x8BD5;&#x53D8;&#x91CF;&#x7C7B;&#x578B;&#x7684;&#x5F71;&#x54CD;&#x51C6;&#x786E;&#x5730;&#x53CD;&#x6620;&#x4E86;JavaScript&#x7684;&#x8BED;&#x4E49;&#xFF08;&#x6BD4;&#x5982;&#xFF0C;&#x53CC;&#x7B49;&#x53F7;&#x8FD0;&#x7B97;&#x7B26;&#x68C0;&#x67E5;&#x4E24;&#x4E2A;&#x503C;&#x65E0;&#x8BBA;&#x4F60;&#x6307;&#x5B9A;&#x7684;&#x662F;null&#x8FD8;&#x662F;undefined&#xFF0C;&#x7136;&#x800C;&#x4E09;&#x7B49;&#x4E8E;&#x53F7;&#x8FD0;&#x7B97;&#x7B26;&#x4EC5;&#x4EC5;&#x68C0;&#x67E5;&#x6307;&#x5B9A;&#x7684;&#x90A3;&#x4E00;&#x4E2A;&#x503C;&#xFF09;&#x3002;</p>
<h3 id="&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x4E2D;&#x7684;&#x70B9;&#x540D;&#x79F0;">&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x4E2D;&#x7684;&#x70B9;&#x540D;&#x79F0;</h3>
<p>&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x4EE5;&#x524D;&#x4EC5;&#x4EC5;&#x652F;&#x6301;&#x5BF9;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x548C;&#x53C2;&#x6570;&#x7684;&#x68C0;&#x67E5;&#x3002;&#x73B0;&#x5728;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x652F;&#x6301;&#x68C0;&#x67E5;&#x7531;&#x53D8;&#x91CF;&#x6216;&#x53C2;&#x6570;&#x540D;&#x79F0;&#x540E;&#x8DDF;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x8BBF;&#x95EE;&#x5C5E;&#x6027;&#x7EC4;&#x6210;&#x7684;&#x201C;&#x70B9;&#x540D;&#x79F0;&#x201D;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> Options {
    location?: {
        x?: <span class="hljs-built_in">number</span>;
        y?: <span class="hljs-built_in">number</span>;
    };
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">options?: Options</span>) </span>{
    <span class="hljs-keyword">if</span> (options &amp;&amp; options.location &amp;&amp; options.location.x) {
        <span class="hljs-keyword">const</span> x = options.location.x;  <span class="hljs-comment">// x&#x7684;&#x7C7B;&#x578B;&#x662F;number</span>
    }
}
</code></pre>
<p>&#x70B9;&#x540D;&#x79F0;&#x7684;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x548C;&#x7528;&#x6237;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x51FD;&#x6570;&#xFF0C;&#x8FD8;&#x6709;<code>typeof</code>&#x548C;<code>instanceof</code>&#x64CD;&#x4F5C;&#x7B26;&#x4E00;&#x8D77;&#x5DE5;&#x4F5C;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x4F9D;&#x8D56;<code>--strictNullChecks</code>&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x5BF9;&#x70B9;&#x540D;&#x79F0;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x540E;&#x7ED9;&#x70B9;&#x540D;&#x79F0;&#x4EFB;&#x4E00;&#x90E8;&#x5206;&#x8D4B;&#x503C;&#x90FD;&#x4F1A;&#x5BFC;&#x81F4;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x65E0;&#x6548;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5BF9;<code>x.y.z</code>&#x8FDB;&#x884C;&#x4E86;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x540E;&#x7ED9;<code>x</code>&#x3001;<code>x.y</code>&#x6216;<code>x.y.z</code>&#x8D4B;&#x503C;&#xFF0C;&#x90FD;&#x4F1A;&#x5BFC;&#x81F4;<code>x.y.z</code>&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x65E0;&#x6548;&#x3002;</p>
<h3 id="&#x8868;&#x8FBE;&#x5F0F;&#x64CD;&#x4F5C;&#x7B26;">&#x8868;&#x8FBE;&#x5F0F;&#x64CD;&#x4F5C;&#x7B26;</h3>
<p>&#x8868;&#x8FBE;&#x5F0F;&#x64CD;&#x4F5C;&#x7B26;&#x5141;&#x8BB8;&#x8FD0;&#x7B97;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x5305;&#x542B;<code>null</code>&#x548C;/&#x6216;<code>undefined</code>&#xFF0C;&#x4F46;&#x662F;&#x603B;&#x662F;&#x4EA7;&#x751F;&#x975E;null&#x548C;&#x975E;undefined&#x7C7B;&#x578B;&#x7684;&#x7ED3;&#x679C;&#x503C;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x4F7F;&#x7528;--strictNullChecks&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x7F16;&#x8BD1;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sum</span>(<span class="hljs-params">a: number | null, b: number | null</span>) </span>{
    <span class="hljs-keyword">return</span> a + b;  <span class="hljs-comment">// &#x8BA1;&#x7B97;&#x7684;&#x7ED3;&#x679C;&#x503C;&#x7C7B;&#x578B;&#x662F;number</span>
}
</code></pre>
<p><code>&amp;&amp;</code>&#x64CD;&#x4F5C;&#x7B26;&#x6DFB;&#x52A0;<code>null</code>&#x548C;/&#x6216;<code>undefined</code>&#x5230;&#x53F3;&#x8FB9;&#x64CD;&#x4F5C;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x4E2D;&#x53D6;&#x51B3;&#x4E8E;&#x5F53;&#x524D;&#x5DE6;&#x8FB9;&#x64CD;&#x4F5C;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#xFF0C;<code>||</code>&#x64CD;&#x4F5C;&#x7B26;&#x4ECE;&#x5DE6;&#x8FB9;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x64CD;&#x4F5C;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x4E2D;&#x5C06;<code>null</code>&#x548C;<code>undefined</code>&#x540C;&#x65F6;&#x5220;&#x9664;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// &#x4F7F;&#x7528;--strictNullChecks&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x7F16;&#x8BD1;</span>
<span class="hljs-keyword">interface</span> Entity {
    name: <span class="hljs-built_in">string</span>;
}
<span class="hljs-keyword">let</span> x: Entity | <span class="hljs-literal">null</span>;
<span class="hljs-keyword">let</span> s = x &amp;&amp; x.name;  <span class="hljs-comment">// s&#x7684;&#x7C7B;&#x578B;&#x662F;string | null</span>
<span class="hljs-keyword">let</span> y = x || { name: <span class="hljs-string">&quot;test&quot;</span> };  <span class="hljs-comment">// y&#x7684;&#x7C7B;&#x578B;&#x662F;Entity</span>
</code></pre>
<h3 id="&#x7C7B;&#x578B;&#x6269;&#x5C55;">&#x7C7B;&#x578B;&#x6269;&#x5C55;</h3>
<p>&#x5728;&#x4E25;&#x683C;&#x7A7A;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;<code>null</code>&#x548C;<code>undefined</code>&#x7C7B;&#x578B;&#x662F;<em>&#x4E0D;&#x4F1A;</em>&#x6269;&#x5C55;&#x5230;<code>any</code>&#x7C7B;&#x578B;&#x4E2D;&#x7684;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> z = <span class="hljs-literal">null</span>;  <span class="hljs-comment">// z&#x7684;&#x7C7B;&#x578B;&#x662F;null</span>
</code></pre>
<p>&#x5728;&#x5E38;&#x89C4;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;&#x7531;&#x4E8E;&#x6269;&#x5C55;&#xFF0C;&#x4F1A;&#x63A8;&#x65AD;<code>z</code>&#x7684;&#x7C7B;&#x578B;&#x662F;<code>any</code>&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x4E25;&#x683C;&#x7A7A;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;&#x63A8;&#x65AD;<code>z</code>&#x662F;<code>null</code>&#x7C7B;&#x578B;&#xFF08;&#x56E0;&#x6B64;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7C7B;&#x578B;&#x6CE8;&#x91CA;&#xFF0C;<code>null</code>&#x662F;<code>z</code>&#x7684;&#x552F;&#x4E00;&#x503C;&#xFF09;&#x3002;</p>
<h3 id="&#x975E;&#x7A7A;&#x65AD;&#x8A00;&#x64CD;&#x4F5C;&#x7B26;">&#x975E;&#x7A7A;&#x65AD;&#x8A00;&#x64CD;&#x4F5C;&#x7B26;</h3>
<p>&#x5728;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x5F53;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x5668;&#x65E0;&#x6CD5;&#x65AD;&#x5B9A;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x64CD;&#x4F5C;&#x7B26;<code>!</code>&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x65AD;&#x8A00;&#x64CD;&#x4F5C;&#x5BF9;&#x8C61;&#x662F;&#x975E;null&#x548C;&#x975E;undefined&#x7C7B;&#x578B;&#x7684;&#x3002;&#x5177;&#x4F53;&#x800C;&#x8A00;&#xFF0C;&#x8FD0;&#x7B97;<code>x!</code>&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x4E0D;&#x5305;&#x542B;<code>null</code>&#x548C;<code>undefined</code>&#x7684;<code>x</code>&#x7684;&#x503C;&#x3002;&#x65AD;&#x8A00;&#x7684;&#x5F62;&#x5F0F;&#x7C7B;&#x4F3C;&#x4E8E;<code>&lt;T&gt;x</code>&#x548C;<code>x as T</code>&#xFF0C;<code>!</code>&#x975E;&#x7A7A;&#x65AD;&#x8A00;&#x64CD;&#x4F5C;&#x7B26;&#x4F1A;&#x4ECE;&#x7F16;&#x8BD1;&#x6210;&#x7684;JavaScript&#x4EE3;&#x7801;&#x4E2D;&#x79FB;&#x9664;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// &#x4F7F;&#x7528;--strictNullChecks&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x7F16;&#x8BD1;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">validateEntity</span>(<span class="hljs-params">e?: Entity</span>) </span>{
    <span class="hljs-comment">// &#x5982;&#x679C;e&#x662F;null&#x6216;&#x8005;&#x65E0;&#x6548;&#x7684;&#x5B9E;&#x4F53;&#xFF0C;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">processEntity</span>(<span class="hljs-params">e?: Entity</span>) </span>{
    validateEntity(e);
    <span class="hljs-keyword">let</span> s = e!.name;  <span class="hljs-comment">// &#x65AD;&#x8A00;e&#x662F;&#x975E;&#x7A7A;&#x5E76;&#x8BBF;&#x95EE;name&#x5C5E;&#x6027;</span>
}
</code></pre>
<h3 id="&#x517C;&#x5BB9;&#x6027;">&#x517C;&#x5BB9;&#x6027;</h3>
<p>&#x8FD9;&#x4E9B;&#x65B0;&#x7279;&#x6027;&#x662F;&#x7ECF;&#x8FC7;&#x8BBE;&#x8BA1;&#x7684;&#xFF0C;&#x4F7F;&#x5F97;&#x5B83;&#x4EEC;&#x80FD;&#x591F;&#x5728;&#x4E25;&#x683C;&#x7A7A;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x548C;&#x5E38;&#x89C4;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E0B;&#x90FD;&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x3002;&#x5C24;&#x5176;&#x662F;&#x5728;&#x5E38;&#x89C4;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;<code>null</code>&#x548C;<code>undefined</code>&#x7C7B;&#x578B;&#x4F1A;&#x81EA;&#x52A8;&#x4ECE;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x4E2D;&#x5220;&#x9664;&#xFF08;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x662F;&#x5176;&#x5B83;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x5B50;&#x7C7B;&#x578B;&#xFF09;&#xFF0C;<code>!</code>&#x975E;&#x7A7A;&#x65AD;&#x8A00;&#x8868;&#x8FBE;&#x5F0F;&#x64CD;&#x4F5C;&#x7B26;&#x4E5F;&#x88AB;&#x5141;&#x8BB8;&#x4F7F;&#x7528;&#x4F46;&#x662F;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x4F5C;&#x7528;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x4F7F;&#x7528;null&#x548C;undefined&#x654F;&#x611F;&#x7C7B;&#x578B;&#x66F4;&#x65B0;&#x540E;&#xFF0C;&#x5728;&#x5E38;&#x89C4;&#x7C7B;&#x578B;&#x6A21;&#x5F0F;&#x4E2D;&#x4ECD;&#x7136;&#x662F;&#x53EF;&#x4EE5;&#x5411;&#x540E;&#x517C;&#x5BB9;&#x4F7F;&#x7528;&#x7684;&#x3002;</p>
<p>&#x5728;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x4E25;&#x683C;&#x7A7A;&#x68C0;&#x67E5;&#x6A21;&#x5F0F;&#x8981;&#x6C42;&#x7F16;&#x8BD1;&#x7684;&#x6240;&#x6709;&#x6587;&#x4EF6;&#x90FD;&#x662F;null&#x548C;undefined&#x654F;&#x611F;&#x7C7B;&#x578B;&#x3002;</p>
<h2 id="&#x57FA;&#x4E8E;&#x63A7;&#x5236;&#x6D41;&#x7684;&#x7C7B;&#x578B;&#x5206;&#x6790;">&#x57FA;&#x4E8E;&#x63A7;&#x5236;&#x6D41;&#x7684;&#x7C7B;&#x578B;&#x5206;&#x6790;</h2>
<p>TypeScript 2.0&#x5B9E;&#x73B0;&#x4E86;&#x5BF9;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x548C;&#x53C2;&#x6570;&#x7684;&#x63A7;&#x5236;&#x6D41;&#x7C7B;&#x578B;&#x5206;&#x6790;&#x3002;&#x4EE5;&#x524D;&#xFF0C;&#x5BF9;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x5206;&#x6790;&#x4EC5;&#x9650;&#x4E8E;<code>if</code>&#x8BED;&#x53E5;&#x548C;<code>?:</code>&#x6761;&#x4EF6;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x5305;&#x62EC;&#x8D4B;&#x503C;&#x548C;&#x63A7;&#x5236;&#x6D41;&#x7ED3;&#x6784;&#x7684;&#x5F71;&#x54CD;&#xFF0C;&#x4F8B;&#x5982;<code>return</code>&#x548C;<code>break</code>&#x8BED;&#x53E5;&#x3002;&#x4F7F;&#x7528;TypeScript 2.0&#xFF0C;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x5668;&#x4F1A;&#x5206;&#x6790;&#x8BED;&#x53E5;&#x548C;&#x8868;&#x8FBE;&#x5F0F;&#x6240;&#x6709;&#x53EF;&#x80FD;&#x7684;&#x63A7;&#x5236;&#x6D41;&#xFF0C;&#x5728;&#x4EFB;&#x4F55;&#x6307;&#x5B9A;&#x7684;&#x4F4D;&#x7F6E;&#x5BF9;&#x58F0;&#x660E;&#x4E3A;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x6216;&#x53C2;&#x6570;&#x4EA7;&#x751F;&#x6700;&#x53EF;&#x80FD;&#x7684;&#x5177;&#x4F53;&#x7C7B;&#x578B;&#xFF08;&#x7F29;&#x5C0F;&#x8303;&#x56F4;&#x7684;&#x7C7B;&#x578B;&#xFF09;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">x: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span> | <span class="hljs-built_in">boolean</span></span>) </span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> x === <span class="hljs-string">&quot;string&quot;</span>) {
        x; <span class="hljs-comment">// &#x8FD9;&#x91CC;x&#x7684;&#x7C7B;&#x578B;&#x662F;string</span>
        x = <span class="hljs-number">1</span>;
        x; <span class="hljs-comment">// &#x8FD9;&#x91CC;x&#x7684;&#x7C7B;&#x578B;&#x662F;number</span>
    }
    x; <span class="hljs-comment">// &#x8FD9;&#x91CC;x&#x7684;&#x7C7B;&#x578B;&#x662F;number | boolean</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">bar</span>(<span class="hljs-params">x: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span></span>) </span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> x === <span class="hljs-string">&quot;number&quot;</span>) {
        <span class="hljs-keyword">return</span>;
    }
    x; <span class="hljs-comment">// &#x8FD9;&#x91CC;x&#x7684;&#x7C7B;&#x578B;&#x662F;string</span>
}
</code></pre>
<p>&#x57FA;&#x4E8E;&#x63A7;&#x5236;&#x6D41;&#x7684;&#x7C7B;&#x578B;&#x5206;&#x6790;&#x5728;<code>--strictNullChecks</code>&#x6A21;&#x5F0F;&#x4E2D;&#x5C24;&#x4E3A;&#x91CD;&#x8981;&#xFF0C;&#x56E0;&#x4E3A;&#x53EF;&#x7A7A;&#x7C7B;&#x578B;&#x4F7F;&#x7528;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x6765;&#x8868;&#x793A;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test</span>(<span class="hljs-params">x: <span class="hljs-built_in">string</span> | <span class="hljs-literal">null</span></span>) </span>{
    <span class="hljs-keyword">if</span> (x === <span class="hljs-literal">null</span>) {
        <span class="hljs-keyword">return</span>;
    }
    x; <span class="hljs-comment">// &#x5728;&#x51FD;&#x6570;&#x7684;&#x5269;&#x4F59;&#x90E8;&#x5206;&#x4E2D;&#xFF0C;x&#x7C7B;&#x578B;&#x662F;string</span>
}
</code></pre>
<p>&#x800C;&#x4E14;&#xFF0C;&#x5728;<code>--strictNullChecks</code>&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;&#x57FA;&#x4E8E;&#x63A7;&#x5236;&#x6D41;&#x7684;&#x5206;&#x6790;&#x5305;&#x62EC;&#xFF0C;&#x5BF9;&#x7C7B;&#x578B;&#x4E0D;&#x5141;&#x8BB8;&#x4E3A;<code>undefined</code>&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x6709;<em>&#x660E;&#x786E;&#x8D4B;&#x503C;</em>&#x7684;&#x5206;&#x6790;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mumble</span>(<span class="hljs-params">check: <span class="hljs-built_in">boolean</span></span>) </span>{
    <span class="hljs-keyword">let</span> x: <span class="hljs-built_in">number</span>; <span class="hljs-comment">// &#x7C7B;&#x578B;&#x4E0D;&#x5141;&#x8BB8;&#x4E3A;undefined</span>
    x; <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;x&#x662F;undefined</span>
    <span class="hljs-keyword">if</span> (check) {
        x = <span class="hljs-number">1</span>;
        x; <span class="hljs-comment">// &#x6B63;&#x786E;</span>
    }
    x; <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;x&#x53EF;&#x80FD;&#x662F;undefi</span>
    x = <span class="hljs-number">2</span>;
    x; <span class="hljs-comment">// &#x6B63;&#x786E;</span>
}
</code></pre>
<h2 id="&#x6807;&#x8BB0;&#x8054;&#x5408;&#x7C7B;&#x578B;">&#x6807;&#x8BB0;&#x8054;&#x5408;&#x7C7B;&#x578B;</h2>
<p>TypeScript 2.0&#x5B9E;&#x73B0;&#x4E86;&#x6807;&#x8BB0;&#xFF08;&#x6216;&#x533A;&#x5206;&#xFF09;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x3002;&#x5177;&#x4F53;&#x800C;&#x8A00;&#xFF0C;TS&#x7F16;&#x8BD1;&#x5668;&#x73B0;&#x5728;&#x652F;&#x6301;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#xFF0C;&#x57FA;&#x4E8E;&#x5224;&#x522B;&#x5C5E;&#x6027;&#x7684;&#x68C0;&#x67E5;&#x6765;&#x7F29;&#x5C0F;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x8303;&#x56F4;&#xFF0C;&#x5E76;&#x4E14;<code>switch</code>&#x8BED;&#x53E5;&#x4E5F;&#x652F;&#x6301;&#x6B64;&#x7279;&#x6027;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> Square {
    kind: <span class="hljs-string">&quot;square&quot;</span>;
    size: <span class="hljs-built_in">number</span>;
}

<span class="hljs-keyword">interface</span> Rectangle {
    kind: <span class="hljs-string">&quot;rectangle&quot;</span>;
    width: <span class="hljs-built_in">number</span>;
    height: <span class="hljs-built_in">number</span>;
}

<span class="hljs-keyword">interface</span> Circle {
    kind: <span class="hljs-string">&quot;circle&quot;</span>;
    radius: <span class="hljs-built_in">number</span>;
}

<span class="hljs-keyword">type</span> Shape = Square | Rectangle | Circle;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">area</span>(<span class="hljs-params">s: Shape</span>) </span>{
    <span class="hljs-comment">// &#x5728;&#x4E0B;&#x9762;&#x7684;switch&#x8BED;&#x53E5;&#x4E2D;&#xFF0C;s&#x7684;&#x7C7B;&#x578B;&#x5728;&#x6BCF;&#x4E00;&#x4E2A;case&#x4E2D;&#x90FD;&#x88AB;&#x7F29;&#x5C0F;</span>
    <span class="hljs-comment">// &#x6839;&#x636E;&#x5224;&#x522B;&#x5C5E;&#x6027;&#x7684;&#x503C;&#xFF0C;&#x53D8;&#x91CF;&#x7684;&#x5176;&#x5B83;&#x5C5E;&#x6027;&#x4E0D;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x65AD;&#x8A00;&#x5C31;&#x53EF;&#x4EE5;&#x88AB;&#x8BBF;&#x95EE;</span>
    <span class="hljs-keyword">switch</span> (s.kind) {
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;square&quot;</span>: <span class="hljs-keyword">return</span> s.size * s.size;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;rectangle&quot;</span>: <span class="hljs-keyword">return</span> s.width * s.height;
        <span class="hljs-keyword">case</span> <span class="hljs-string">&quot;circle&quot;</span>: <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.PI * s.radius * s.radius;
    }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test1</span>(<span class="hljs-params">s: Shape</span>) </span>{
    <span class="hljs-keyword">if</span> (s.kind === <span class="hljs-string">&quot;square&quot;</span>) {
        s;  <span class="hljs-comment">// Square</span>
    }
    <span class="hljs-keyword">else</span> {
        s;  <span class="hljs-comment">// Rectangle | Circle</span>
    }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test2</span>(<span class="hljs-params">s: Shape</span>) </span>{
    <span class="hljs-keyword">if</span> (s.kind === <span class="hljs-string">&quot;square&quot;</span> || s.kind === <span class="hljs-string">&quot;rectangle&quot;</span>) {
        <span class="hljs-keyword">return</span>;
    }
    s;  <span class="hljs-comment">// Circle</span>
}
</code></pre>
<p><em>&#x5224;&#x522B;&#x5C5E;&#x6027;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;</em>&#x662F;<code>x.p == v</code>&#x3001;<code>x.p === v</code>&#x3001;<code>x.p != v</code>&#x6216;&#x8005;<code>x.p !== v</code>&#x5176;&#x4E2D;&#x7684;&#x4E00;&#x79CD;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;<code>p</code>&#x548C;<code>v</code>&#x662F;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x6216;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x5224;&#x522B;&#x5C5E;&#x6027;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x7F29;&#x5C0F;<code>x</code>&#x7684;&#x7C7B;&#x578B;&#x5230;&#x7531;&#x5224;&#x522B;&#x5C5E;&#x6027;<code>p</code>&#x548C;<code>v</code>&#x7684;&#x53EF;&#x80FD;&#x503C;&#x4E4B;&#x4E00;&#x7EC4;&#x6210;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x6211;&#x4EEC;&#x76EE;&#x524D;&#x53EA;&#x652F;&#x6301;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x5224;&#x522B;&#x5C5E;&#x6027;&#x3002;&#x6211;&#x4EEC;&#x6253;&#x7B97;&#x4EE5;&#x540E;&#x6DFB;&#x52A0;&#x5BF9;&#x5E03;&#x5C14;&#x503C;&#x548C;&#x6570;&#x5B57;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x7684;&#x652F;&#x6301;&#x3002;</p>
<h2 id="never&#x7C7B;&#x578B;"><code>never</code>&#x7C7B;&#x578B;</h2>
<p>TypeScript 2.0&#x5F15;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x539F;&#x59CB;&#x7C7B;&#x578B;<code>never</code>&#x3002;<code>never</code>&#x7C7B;&#x578B;&#x8868;&#x793A;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x4ECE;&#x4E0D;&#x51FA;&#x73B0;&#x3002;&#x5177;&#x4F53;&#x800C;&#x8A00;&#xFF0C;<code>never</code>&#x662F;&#x6C38;&#x4E0D;&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#xFF0C;&#x4E5F;&#x662F;&#x53D8;&#x91CF;&#x5728;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x4E2D;&#x6C38;&#x4E0D;&#x4E3A;true&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<p><code>never</code>&#x7C7B;&#x578B;&#x5177;&#x6709;&#x4EE5;&#x4E0B;&#x7279;&#x5F81;&#xFF1A;</p>
<ul>
<li><code>never</code>&#x662F;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x5B50;&#x7C7B;&#x578B;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x3002;</li>
<li>&#x6CA1;&#x6709;&#x7C7B;&#x578B;&#x662F;<code>never</code>&#x7684;&#x5B50;&#x7C7B;&#x578B;&#x6216;&#x80FD;&#x8D4B;&#x503C;&#x7ED9;<code>never</code>&#xFF08;<code>never</code>&#x7C7B;&#x578B;&#x672C;&#x8EAB;&#x9664;&#x5916;&#xFF09;&#x3002;</li>
<li>&#x5728;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x6216;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x51FD;&#x6570;&#x6CA1;&#x6709;<code>return</code>&#x8BED;&#x53E5;&#xFF0C;&#x6216;&#x8005;&#x53EA;&#x6709;<code>never</code>&#x7C7B;&#x578B;&#x8868;&#x8FBE;&#x5F0F;&#x7684;<code>return</code>&#x8BED;&#x53E5;&#xFF0C;&#x5E76;&#x4E14;&#x5982;&#x679C;&#x51FD;&#x6570;&#x662F;&#x4E0D;&#x53EF;&#x6267;&#x884C;&#x5230;&#x7EC8;&#x70B9;&#x7684;&#xFF08;&#x4F8B;&#x5982;&#x901A;&#x8FC7;&#x63A7;&#x5236;&#x6D41;&#x5206;&#x6790;&#x51B3;&#x5B9A;&#x7684;&#xFF09;&#xFF0C;&#x5219;&#x63A8;&#x65AD;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x662F;<code>never</code>&#x3002;</li>
<li>&#x5728;&#x6709;&#x660E;&#x786E;<code>never</code>&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x7684;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x6240;&#x6709;<code>return</code>&#x8BED;&#x53E5;&#xFF08;&#x5982;&#x679C;&#x6709;&#x7684;&#x8BDD;&#xFF09;&#x5FC5;&#x987B;&#x6709;<code>never</code>&#x7C7B;&#x578B;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x5E76;&#x4E14;&#x51FD;&#x6570;&#x7684;&#x7EC8;&#x70B9;&#x5FC5;&#x987B;&#x662F;&#x4E0D;&#x53EF;&#x6267;&#x884C;&#x7684;&#x3002;</li>
</ul>
<p>&#x56E0;&#x4E3A;<code>never</code>&#x662F;&#x6BCF;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x5B50;&#x7C7B;&#x578B;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x603B;&#x662F;&#x5728;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x4E2D;&#x88AB;&#x7701;&#x7565;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x53EA;&#x8981;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#x88AB;&#x8FD4;&#x56DE;&#xFF0C;&#x7C7B;&#x578B;&#x63A8;&#x65AD;&#x5C31;&#x4F1A;&#x5FFD;&#x7565;<code>never</code>&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x4E00;&#x4E9B;&#x8FD4;&#x56DE;<code>never</code>&#x51FD;&#x6570;&#x7684;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// &#x51FD;&#x6570;&#x8FD4;&#x56DE;never&#x5FC5;&#x987B;&#x65E0;&#x6CD5;&#x6267;&#x884C;&#x5230;&#x7EC8;&#x70B9;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">error</span>(<span class="hljs-params">message: <span class="hljs-built_in">string</span></span>): <span class="hljs-title">never</span> </span>{
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(message);
}

<span class="hljs-comment">// &#x63A8;&#x65AD;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x662F;never</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fail</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> error(<span class="hljs-string">&quot;Something failed&quot;</span>);
}

<span class="hljs-comment">// &#x51FD;&#x6570;&#x8FD4;&#x56DE;never&#x5FC5;&#x987B;&#x65E0;&#x6CD5;&#x6267;&#x884C;&#x5230;&#x7EC8;&#x70B9;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">infiniteLoop</span>(<span class="hljs-params"></span>): <span class="hljs-title">never</span> </span>{
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
    }
}
</code></pre>
<p>&#x4E00;&#x4E9B;&#x51FD;&#x6570;&#x8FD4;&#x56DE;<code>never</code>&#x7684;&#x4F7F;&#x7528;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-typescript">// &#x63A8;&#x65AD;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x662F;number
function move1(direction: &quot;up&quot; | &quot;down&quot;) {
    switch (direction) {
        case &quot;up&quot;:
            return 1;
        case &quot;down&quot;:
            return -1;
    }
    return error(&quot;Should never get here&quot;);
}

// &#x63A8;&#x65AD;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x662F;number
function move2(direction: &quot;up&quot; | &quot;down&quot;) {
    return direction === &quot;up&quot; ? 1 :
        direction === &quot;down&quot; ? -1 :
        error(&quot;Should never get here&quot;);
}

// &#x63A8;&#x65AD;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x662F;T
function check&lt;T&gt;(x: T | undefined) {
    return x || error(&quot;Undefined value&quot;);
}
</code></pre>
<p>&#x56E0;&#x4E3A;<code>never</code>&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;&#x6BCF;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#xFF0C;&#x5F53;&#x9700;&#x8981;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x66F4;&#x52A0;&#x5177;&#x4F53;&#x7684;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x51FD;&#x6570;&#x8FD4;&#x56DE;<code>never</code>&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x68C0;&#x6D4B;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x662F;&#x5426;&#x6B63;&#x786E;&#xFF1A;</p>
<pre><code class="lang-typescript">function test(cb: () =&gt; string) {
    let s = cb();
    return s;
}

test(() =&gt; &quot;hello&quot;);
test(() =&gt; fail());
test(() =&gt; { throw new Error(); })
</code></pre>
<h2 id="&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#x548C;&#x7D22;&#x5F15;&#x7B7E;&#x540D;">&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#x548C;&#x7D22;&#x5F15;&#x7B7E;&#x540D;</h2>
<p>&#x5C5E;&#x6027;&#x6216;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>readonly</code>&#x4FEE;&#x9970;&#x7B26;&#x58F0;&#x660E;&#x4E3A;&#x53EA;&#x8BFB;&#x7684;&#x3002;</p>
<p>&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x521D;&#x59CB;&#x5316;&#x548C;&#x5728;&#x540C;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x88AB;&#x8D4B;&#x503C;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x5176;&#x5B83;&#x60C5;&#x51B5;&#x4E0B;&#x5BF9;&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#x7684;&#x8D4B;&#x503C;&#x662F;&#x4E0D;&#x5141;&#x8BB8;&#x7684;&#x3002;</p>
<p>&#x6B64;&#x5916;&#xFF0C;&#x6709;&#x51E0;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x5B9E;&#x4F53;<em>&#x9690;&#x5F0F;</em>&#x53EA;&#x8BFB;&#x7684;&#xFF1A;</p>
<ul>
<li>&#x5C5E;&#x6027;&#x58F0;&#x660E;&#x53EA;&#x4F7F;&#x7528;<code>get</code>&#x8BBF;&#x95EE;&#x5668;&#x800C;&#x6CA1;&#x6709;&#x4F7F;&#x7528;<code>set</code>&#x8BBF;&#x95EE;&#x5668;&#x88AB;&#x89C6;&#x4E3A;&#x53EA;&#x8BFB;&#x7684;&#x3002;</li>
<li>&#x5728;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x4E2D;&#xFF0C;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x88AB;&#x89C6;&#x4E3A;&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#x3002;</li>
<li>&#x5728;&#x6A21;&#x5757;&#x7C7B;&#x578B;&#x4E2D;&#xFF0C;&#x5BFC;&#x51FA;&#x7684;<code>const</code>&#x53D8;&#x91CF;&#x88AB;&#x89C6;&#x4E3A;&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#x3002;</li>
<li>&#x5728;<code>import</code>&#x8BED;&#x53E5;&#x4E2D;&#x58F0;&#x660E;&#x7684;&#x5B9E;&#x4F53;&#x88AB;&#x89C6;&#x4E3A;&#x53EA;&#x8BFB;&#x7684;&#x3002;</li>
<li>&#x901A;&#x8FC7;ES2015&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5BFC;&#x5165;&#x8BBF;&#x95EE;&#x7684;&#x5B9E;&#x4F53;&#x88AB;&#x89C6;&#x4E3A;&#x53EA;&#x8BFB;&#x7684;&#xFF08;&#x4F8B;&#x5982;&#xFF0C;&#x5F53;<code>foo</code>&#x5F53;&#x4F5C;<code>import * as foo from &quot;foo&quot;</code>&#x58F0;&#x660E;&#x65F6;&#xFF0C;<code>foo.x</code>&#x662F;&#x53EA;&#x8BFB;&#x7684;&#xFF09;&#x3002;</li>
</ul>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> Point {
    readonly x: <span class="hljs-built_in">number</span>;
    readonly y: <span class="hljs-built_in">number</span>;
}

<span class="hljs-keyword">var</span> p1: Point = { x: <span class="hljs-number">10</span>, y: <span class="hljs-number">20</span> };
p1.x = <span class="hljs-number">5</span>;  <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;p1.x&#x662F;&#x53EA;&#x8BFB;&#x7684;</span>

<span class="hljs-keyword">var</span> p2 = { x: <span class="hljs-number">1</span>, y: <span class="hljs-number">1</span> };
<span class="hljs-keyword">var</span> p3: Point = p2;  <span class="hljs-comment">// &#x6B63;&#x786E;&#xFF0C;p2&#x7684;&#x53EA;&#x8BFB;&#x522B;&#x540D;</span>
p3.x = <span class="hljs-number">5</span>;  <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;p3.x&#x662F;&#x53EA;&#x8BFB;&#x7684;</span>
p2.x = <span class="hljs-number">5</span>;  <span class="hljs-comment">// &#x6B63;&#x786E;&#xFF0C;&#x4F46;&#x662F;&#x56E0;&#x4E3A;&#x522B;&#x540D;&#x4F7F;&#x7528;&#xFF0C;&#x540C;&#x65F6;&#x4E5F;&#x6539;&#x53D8;&#x4E86;p3.x</span>
</code></pre>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Foo {
    readonly a = <span class="hljs-number">1</span>;
    readonly b: <span class="hljs-built_in">string</span>;
    <span class="hljs-keyword">constructor</span>() {
        <span class="hljs-keyword">this</span>.b = <span class="hljs-string">&quot;hello&quot;</span>;  <span class="hljs-comment">// &#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x5141;&#x8BB8;&#x8D4B;&#x503C;</span>
    }
}
</code></pre>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> a: <span class="hljs-built_in">Array</span>&lt;<span class="hljs-built_in">number</span>&gt; = [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
<span class="hljs-keyword">let</span> b: ReadonlyArray&lt;<span class="hljs-built_in">number</span>&gt; = a;
b[<span class="hljs-number">5</span>] = <span class="hljs-number">5</span>;      <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;&#x5143;&#x7D20;&#x662F;&#x53EA;&#x8BFB;&#x7684;</span>
b.push(<span class="hljs-number">5</span>);     <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;&#x6CA1;&#x6709;push&#x65B9;&#x6CD5;&#xFF08;&#x56E0;&#x4E3A;&#x8FD9;&#x4F1A;&#x4FEE;&#x6539;&#x6570;&#x7EC4;&#xFF09;</span>
b.length = <span class="hljs-number">3</span>;  <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;length&#x662F;&#x53EA;&#x8BFB;&#x7684;</span>
a = b;         <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF0C;&#x7F3A;&#x5C11;&#x4FEE;&#x6539;&#x6570;&#x7EC4;&#x7684;&#x65B9;&#x6CD5;</span>
</code></pre>
<h2 id="&#x6307;&#x5B9A;&#x51FD;&#x6570;&#x4E2D;this&#x7C7B;&#x578B;">&#x6307;&#x5B9A;&#x51FD;&#x6570;&#x4E2D;<code>this</code>&#x7C7B;&#x578B;</h2>
<p>&#x7D27;&#x8DDF;&#x7740;&#x7C7B;&#x548C;&#x63A5;&#x53E3;&#xFF0C;&#x73B0;&#x5728;&#x51FD;&#x6570;&#x548C;&#x65B9;&#x6CD5;&#x4E5F;&#x53EF;&#x4EE5;&#x58F0;&#x660E;<code>this</code>&#x7684;&#x7C7B;&#x578B;&#x4E86;&#x3002;</p>
<p>&#x51FD;&#x6570;&#x4E2D;<code>this</code>&#x7684;&#x9ED8;&#x8BA4;&#x7C7B;&#x578B;&#x662F;<code>any</code>&#x3002;&#x4ECE;TypeScript 2.0&#x5F00;&#x59CB;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x660E;&#x786E;&#x7684;<code>this</code>&#x53C2;&#x6570;&#x3002;<code>this</code>&#x53C2;&#x6570;&#x662F;&#x4F2A;&#x53C2;&#x6570;&#xFF0C;&#x5B83;&#x4F4D;&#x4E8E;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x5217;&#x8868;&#x7684;&#x7B2C;&#x4E00;&#x4F4D;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: <span class="hljs-built_in">void</span></span>) </span>{
    <span class="hljs-comment">// &#x786E;&#x4FDD;`this`&#x5728;&#x8FD9;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;&#x51FD;&#x6570;&#x4E2D;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;</span>
}
</code></pre>
<h3 id="&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#x7684;this&#x53C2;&#x6570;">&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#x7684;<code>this</code>&#x53C2;&#x6570;</h3>
<p>&#x5E93;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>this</code>&#x53C2;&#x6570;&#x58F0;&#x660E;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5982;&#x4F55;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> UIElement {
    addClickListener(onclick: (<span class="hljs-keyword">this</span>: <span class="hljs-built_in">void</span>, e: Event) =&gt; <span class="hljs-built_in">void</span>): <span class="hljs-built_in">void</span>;
}
</code></pre>
<p><code>this:void</code>&#x610F;&#x5473;&#x7740;<code>addClickListener</code>&#x9884;&#x8BA1;<code>onclick</code>&#x662F;&#x4E00;&#x4E2A;<code>this</code>&#x53C2;&#x6570;&#x4E0D;&#x9700;&#x8981;&#x7C7B;&#x578B;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x5982;&#x679C;&#x4F60;&#x5728;&#x8C03;&#x7528;&#x4EE3;&#x7801;&#x4E2D;&#x5BF9;<code>this</code>&#x8FDB;&#x884C;&#x4E86;&#x7C7B;&#x578B;&#x6CE8;&#x91CA;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Handler {
    info: <span class="hljs-built_in">string</span>;
    onClickBad(<span class="hljs-keyword">this</span>: Handler, e: Event) {
        <span class="hljs-comment">// &#x54CE;&#x54DF;&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;this.&#x5728;&#x8FD0;&#x884C;&#x4E2D;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5C06;&#x4F1A;&#x5D29;&#x6E83;&#x3002;</span>
        <span class="hljs-keyword">this</span>.info = e.message;
    };
}
<span class="hljs-keyword">let</span> h = <span class="hljs-keyword">new</span> Handler();
uiElement.addClickListener(h.onClickBad); <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF01;</span>
</code></pre>
<h3 id="-noimplicitthis"><code>--noImplicitThis</code></h3>
<p>TypeScript 2.0&#x8FD8;&#x589E;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7F16;&#x8BD1;&#x9009;&#x9879;&#x7528;&#x6765;&#x6807;&#x8BB0;&#x51FD;&#x6570;&#x4E2D;&#x6240;&#x6709;&#x6CA1;&#x6709;&#x660E;&#x786E;&#x7C7B;&#x578B;&#x6CE8;&#x91CA;&#x7684;<code>this</code>&#x7684;&#x4F7F;&#x7528;&#x3002;</p>
<h2 id="tsconfigjson&#x652F;&#x6301;&#x6587;&#x4EF6;&#x901A;&#x914D;&#x7B26;"><code>tsconfig.json</code>&#x652F;&#x6301;&#x6587;&#x4EF6;&#x901A;&#x914D;&#x7B26;</h2>
<p>&#x6587;&#x4EF6;&#x901A;&#x914D;&#x7B26;&#x6765;&#x5566;&#xFF01;&#xFF01;&#x652F;&#x6301;&#x6587;&#x4EF6;&#x901A;&#x914D;&#x7B26;&#x4E00;&#x76F4;&#x662F;<a href="https://github.com/Microsoft/TypeScript/issues/1927" target="_blank">&#x6700;&#x9700;&#x8981;&#x7684;&#x7279;&#x6027;&#x4E4B;&#x4E00;</a>&#x3002;</p>
<p>&#x7C7B;&#x4F3C;&#x6587;&#x4EF6;&#x901A;&#x914D;&#x7B26;&#x7684;&#x6587;&#x4EF6;&#x6A21;&#x5F0F;&#x652F;&#x6301;&#x4E24;&#x4E2A;&#x5C5E;&#x6027;<code>&quot;include&quot;</code>&#x548C;<code>&quot;exclude&quot;</code>&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
        <span class="hljs-string">&quot;module&quot;</span>: <span class="hljs-string">&quot;commonjs&quot;</span>,
        <span class="hljs-string">&quot;noImplicitAny&quot;</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">&quot;removeComments&quot;</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">&quot;preserveConstEnums&quot;</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-string">&quot;outFile&quot;</span>: <span class="hljs-string">&quot;../../built/local/tsc.js&quot;</span>,
        <span class="hljs-string">&quot;sourceMap&quot;</span>: <span class="hljs-literal">true</span>
    },
    <span class="hljs-string">&quot;include&quot;</span>: [
        <span class="hljs-string">&quot;src/**/*&quot;</span>
    ],
    <span class="hljs-string">&quot;exclude&quot;</span>: [
        <span class="hljs-string">&quot;node_modules&quot;</span>,
        <span class="hljs-string">&quot;**/*.spec.ts&quot;</span>
    ]
}
</code></pre>
<p>&#x652F;&#x6301;&#x6587;&#x4EF6;&#x901A;&#x914D;&#x7B26;&#x7684;&#x7B26;&#x53F7;&#x6709;&#xFF1A;</p>
<ul>
<li><code>*</code>&#x5339;&#x914D;&#x96F6;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5B57;&#x7B26;&#xFF08;&#x4E0D;&#x5305;&#x62EC;&#x76EE;&#x5F55;&#xFF09;</li>
<li><code>?</code>&#x5339;&#x914D;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#xFF08;&#x4E0D;&#x5305;&#x62EC;&#x76EE;&#x5F55;&#xFF09;</li>
<li><code>**/</code>&#x9012;&#x5F52;&#x5339;&#x914D;&#x6240;&#x6709;&#x5B50;&#x76EE;&#x5F55;</li>
</ul>
<p>&#x5982;&#x679C;&#x6587;&#x4EF6;&#x901A;&#x914D;&#x7B26;&#x6A21;&#x5F0F;&#x8BED;&#x53E5;&#x4E2D;&#x53EA;&#x5305;&#x542B;<code>*</code>&#x6216;<code>.*</code>&#xFF0C;&#x90A3;&#x4E48;&#x53EA;&#x5339;&#x914D;&#x5E26;&#x6709;&#x6269;&#x5C55;&#x540D;&#x7684;&#x6587;&#x4EF6;&#xFF08;&#x4F8B;&#x5982;&#x9ED8;&#x8BA4;&#x662F;<code>.ts</code>&#x3001;<code>.tsx</code>&#x548C;<code>.d.ts</code>&#xFF0C;&#x5982;&#x679C;<code>allowJs</code>&#x8BBE;&#x7F6E;&#x4E3A;<code>true</code>&#xFF0C;<code>.js</code>&#x548C;<code>.jsx</code>&#x4E5F;&#x5C5E;&#x4E8E;&#x9ED8;&#x8BA4;&#xFF09;&#x3002;</p>
<p>&#x5982;&#x679C;<code>&quot;files&quot;</code>&#x548C;<code>&quot;include&quot;</code>&#x90FD;&#x6CA1;&#x6709;&#x6307;&#x5B9A;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x9ED8;&#x8BA4;&#x5305;&#x542B;&#x6240;&#x6709;&#x76EE;&#x5F55;&#x4E2D;&#x7684;TypeScript&#x6587;&#x4EF6;&#xFF08;<code>.ts</code>&#x3001;<code>.d.ts</code>&#x548C;<code>.tsx</code>&#xFF09;&#xFF0C;&#x9664;&#x4E86;&#x90A3;&#x4E9B;&#x4F7F;&#x7528;<code>exclude</code>&#x5C5E;&#x6027;&#x6392;&#x9664;&#x7684;&#x6587;&#x4EF6;&#x5916;&#x3002;&#x5982;&#x679C;<code>allowJs</code>&#x8BBE;&#x7F6E;&#x4E3A;true&#xFF0C;JS&#x6587;&#x4EF6;&#xFF08;<code>.js</code>&#x548C;<code>.jsx</code>&#xFF09;&#x4E5F;&#x4F1A;&#x88AB;&#x5305;&#x542B;&#x8FDB;&#x53BB;&#x3002;</p>
<p>&#x5982;&#x679C;<code>&quot;files&quot;</code>&#x548C;<code>&quot;include&quot;</code>&#x90FD;&#x6307;&#x5B9A;&#x4E86;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x5C06;&#x5305;&#x542B;&#x8FD9;&#x4E24;&#x4E2A;&#x5C5E;&#x6027;&#x6307;&#x5B9A;&#x6587;&#x4EF6;&#x7684;&#x5E76;&#x96C6;&#x3002;&#x4F7F;&#x7528;<code>ourDir</code>&#x7F16;&#x8BD1;&#x9009;&#x9879;&#x6307;&#x5B9A;&#x7684;&#x76EE;&#x5F55;&#x6587;&#x4EF6;&#x603B;&#x662F;&#x88AB;&#x6392;&#x9664;&#xFF0C;&#x5373;&#x4F7F;<code>&quot;exclude&quot;</code>&#x5C5E;&#x6027;&#x6307;&#x5B9A;&#x7684;&#x6587;&#x4EF6;&#x4E5F;&#x4F1A;&#x88AB;&#x5220;&#x9664;&#xFF0C;&#x4F46;&#x662F;<code>files</code>&#x5C5E;&#x6027;&#x6307;&#x5B9A;&#x7684;&#x6587;&#x4EF6;&#x4E0D;&#x4F1A;&#x6392;&#x9664;&#x3002;</p>
<p><code>&quot;exclude&quot;</code>&#x5C5E;&#x6027;&#x6307;&#x5B9A;&#x7684;&#x6587;&#x4EF6;&#x4F1A;&#x5BF9;<code>&quot;include&quot;</code>&#x5C5E;&#x6027;&#x6307;&#x5B9A;&#x7684;&#x6587;&#x4EF6;&#x8FC7;&#x6EE4;&#x3002;&#x4F46;&#x662F;&#x5BF9;<code>&quot;files&quot;</code>&#x6307;&#x5B9A;&#x7684;&#x6587;&#x4EF6;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x4F5C;&#x7528;&#x3002;&#x5F53;&#x6CA1;&#x6709;&#x660E;&#x786E;&#x6307;&#x5B9A;&#x65F6;&#xFF0C;<code>&quot;exclude&quot;</code>&#x5C5E;&#x6027;&#x9ED8;&#x8BA4;&#x4F1A;&#x6392;&#x9664;<code>node_modules</code>&#x3001;<code>bower_components</code>&#x548C;<code>jspm_packages</code>&#x76EE;&#x5F55;&#x3002;</p>
<h2 id="&#x6A21;&#x5757;&#x89E3;&#x6790;&#x589E;&#x52A0;&#xFF1A;baseurl&#x3001;&#x8DEF;&#x5F84;&#x6620;&#x5C04;&#x3001;rootdirs&#x548C;&#x8FFD;&#x8E2A;">&#x6A21;&#x5757;&#x89E3;&#x6790;&#x589E;&#x52A0;&#xFF1A;BaseUrl&#x3001;&#x8DEF;&#x5F84;&#x6620;&#x5C04;&#x3001;rootDirs&#x548C;&#x8FFD;&#x8E2A;</h2>
<p>TypeScript 2.0&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x989D;&#x5916;&#x7684;&#x6A21;&#x5757;&#x89E3;&#x6790;&#x5C5E;&#x6027;&#x544A;&#x8BC9;&#x7F16;&#x8BD1;&#x5668;&#x53BB;&#x54EA;&#x91CC;&#x53EF;&#x4EE5;&#x627E;&#x5230;&#x7ED9;&#x5B9A;&#x6A21;&#x5757;&#x7684;&#x58F0;&#x660E;&#x3002;</p>
<p>&#x66F4;&#x591A;&#x8BE6;&#x60C5;&#xFF0C;&#x8BF7;&#x53C2;&#x9605;<a href="../handbook/Module%20Resolution.md">&#x6A21;&#x5757;&#x89E3;&#x6790;</a>&#x6587;&#x6863;&#x3002;</p>
<h3 id="base-url">Base URL</h3>
<p>&#x4F7F;&#x7528;&#x4E86;AMD&#x6A21;&#x5757;&#x52A0;&#x8F7D;&#x5668;&#x5E76;&#x4E14;&#x6A21;&#x5757;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x201D;&#x90E8;&#x7F72;&#x201C;&#x5230;&#x5355;&#x6587;&#x4EF6;&#x5939;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#x4F7F;&#x7528;<code>baseUrl</code>&#x662F;&#x4E00;&#x79CD;&#x5E38;&#x7528;&#x7684;&#x505A;&#x6CD5;&#x3002;&#x6240;&#x6709;&#x975E;&#x76F8;&#x5BF9;&#x540D;&#x79F0;&#x7684;&#x6A21;&#x5757;&#x5BFC;&#x5165;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x76F8;&#x5BF9;&#x4E8E;<code>baseUrl</code>&#x7684;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
    <span class="hljs-string">&quot;baseUrl&quot;</span>: <span class="hljs-string">&quot;./modules&quot;</span>
  }
}
</code></pre>
<p>&#x73B0;&#x5728;&#x5BFC;&#x5165;<code>moduleA</code>&#x5C06;&#x4F1A;&#x5728;<code>./modules/moduleA</code>&#x4E2D;&#x67E5;&#x627E;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> A from <span class="hljs-string">&quot;moduleA&quot;</span>;
</code></pre>
<h3 id="&#x8DEF;&#x5F84;&#x6620;&#x5C04;">&#x8DEF;&#x5F84;&#x6620;&#x5C04;</h3>
<p>&#x6709;&#x65F6;&#x6A21;&#x5757;&#x6CA1;&#x6709;&#x76F4;&#x63A5;&#x4F4D;&#x4E8E;<em>baseUrl</em>&#x4E2D;&#x3002;&#x52A0;&#x8F7D;&#x5668;&#x4F7F;&#x7528;&#x6620;&#x5C04;&#x914D;&#x7F6E;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x53BB;&#x6620;&#x5C04;&#x6A21;&#x5757;&#x540D;&#x79F0;&#x548C;&#x6587;&#x4EF6;&#xFF0C;&#x8BF7;&#x53C2;&#x9605;<a href="http://requirejs.org/docs/api.html#config-paths" target="_blank">RequireJs&#x6587;&#x6863;</a>&#x548C;<a href="https://github.com/systemjs/systemjs/blob/master/docs/overview.md#map-config" target="_blank">SystemJS&#x6587;&#x6863;</a>&#x3002;</p>
<p>TypeScript&#x7F16;&#x8BD1;&#x5668;&#x652F;&#x6301;<code>tsconfig</code>&#x6587;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;<code>&quot;paths&quot;</code>&#x5C5E;&#x6027;&#x6620;&#x5C04;&#x7684;&#x58F0;&#x660E;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x5BFC;&#x5165;<code>&quot;jquery&quot;</code>&#x6A21;&#x5757;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x4F1A;&#x88AB;&#x8F6C;&#x6362;&#x4E3A;<code>&quot;node_modules/jquery/dist/jquery.slim.min.js&quot;</code>&#x3002;</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
        <span class="hljs-string">&quot;baseUrl&quot;</span>: <span class="hljs-string">&quot;./node_modules&quot;</span>,
        <span class="hljs-string">&quot;paths&quot;</span>: {
        <span class="hljs-string">&quot;jquery&quot;</span>: [<span class="hljs-string">&quot;jquery/dist/jquery.slim.min&quot;</span>]
        }
    }
}
</code></pre>
<p>&#x4F7F;&#x7528;<code>&quot;paths&quot;</code>&#x4E5F;&#x5141;&#x8BB8;&#x66F4;&#x590D;&#x6742;&#x7684;&#x6620;&#x5C04;&#xFF0C;&#x5305;&#x62EC;&#x591A;&#x6B21;&#x540E;&#x9000;&#x7684;&#x4F4D;&#x7F6E;&#x3002;&#x8003;&#x8651;&#x4E00;&#x4E2A;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5730;&#x65B9;&#x7684;&#x6A21;&#x5757;&#x662F;&#x53EF;&#x7528;&#x7684;&#xFF0C;&#x5176;&#x5B83;&#x7684;&#x6A21;&#x5757;&#x90FD;&#x5728;&#x53E6;&#x4E00;&#x4E2A;&#x5730;&#x65B9;&#x7684;&#x9879;&#x76EE;&#x914D;&#x7F6E;&#x3002;</p>
<h3 id="rootdirs&#x548C;&#x865A;&#x62DF;&#x76EE;&#x5F55;"><code>rootDirs</code>&#x548C;&#x865A;&#x62DF;&#x76EE;&#x5F55;</h3>
<p>&#x4F7F;&#x7528;<code>rootDirs</code>&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x544A;&#x77E5;&#x7F16;&#x8BD1;&#x5668;&#x7684;<em>&#x6839;&#x76EE;&#x5F55;</em>&#x7EC4;&#x5408;&#x8FD9;&#x4E9B;&#x201C;&#x865A;&#x62DF;&#x201D;&#x76EE;&#x5F55;&#x3002;&#x56E0;&#x6B64;&#x7F16;&#x8BD1;&#x5668;&#x5728;&#x8FD9;&#x4E9B;&#x201C;&#x865A;&#x62DF;&#x201D;&#x76EE;&#x5F55;&#x4E2D;&#x89E3;&#x6790;&#x76F8;&#x5BF9;&#x5BFC;&#x5165;&#x6A21;&#x5757;&#xFF0C;&#x4EFF;&#x4F5B;&#x662F;&#x5408;&#x5E76;&#x5230;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#x4E2D;&#x4E00;&#x6837;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<p>&#x7ED9;&#x5B9A;&#x7684;&#x9879;&#x76EE;&#x7ED3;&#x6784;</p>
<pre><code class="lang-tree"> src
 &#x2514;&#x2500;&#x2500; views
     &#x2514;&#x2500;&#x2500; view1.ts (imports &apos;./template1&apos;)
     &#x2514;&#x2500;&#x2500; view2.ts

 generated
 &#x2514;&#x2500;&#x2500; templates
         &#x2514;&#x2500;&#x2500; views
             &#x2514;&#x2500;&#x2500; template1.ts (imports &apos;./view2&apos;)
</code></pre>
<p>&#x6784;&#x5EFA;&#x6B65;&#x9AA4;&#x5C06;&#x590D;&#x5236;<code>/src/views</code>&#x548C;<code>/generated/templates/views</code>&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x6587;&#x4EF6;&#x8F93;&#x51FA;&#x5230;&#x540C;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#x4E2D;&#x3002;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#xFF0C;&#x89C6;&#x56FE;&#x671F;&#x671B;&#x5B83;&#x7684;&#x6A21;&#x677F;&#x548C;&#x5B83;&#x5B58;&#x5728;&#x540C;&#x4E00;&#x76EE;&#x5F55;&#x4E2D;&#xFF0C;&#x56E0;&#x6B64;&#x5E94;&#x8BE5;&#x4F7F;&#x7528;&#x76F8;&#x5BF9;&#x540D;&#x79F0;<code>&quot;./template&quot;</code>&#x5BFC;&#x5165;&#x3002;</p>
<p><code>&quot;rootDir&quot;</code>&#x6307;&#x5B9A;&#x7684;&#x4E00;&#x7EC4;&#x6839;&#x76EE;&#x5F55;&#x7684;&#x5185;&#x5BB9;&#x5C06;&#x4F1A;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x5408;&#x5E76;&#x3002;&#x56E0;&#x6B64;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;<code>tsconfig.json</code>&#x6587;&#x4EF6;&#x5E94;&#x8BE5;&#x7C7B;&#x4F3C;&#x4E8E;&#xFF1A;</p>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
    <span class="hljs-string">&quot;rootDirs&quot;</span>: [
      <span class="hljs-string">&quot;src/views&quot;</span>,
      <span class="hljs-string">&quot;generated/templates/views&quot;</span>
    ]
  }
}
</code></pre>
<h3 id="&#x8FFD;&#x8E2A;&#x6A21;&#x5757;&#x89E3;&#x6790;">&#x8FFD;&#x8E2A;&#x6A21;&#x5757;&#x89E3;&#x6790;</h3>
<p><code>--traceResolution</code>&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x65B9;&#x4FBF;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4EE5;&#x4E86;&#x89E3;&#x6A21;&#x5757;&#x5982;&#x4F55;&#x88AB;&#x7F16;&#x8BD1;&#x5668;&#x89E3;&#x6790;&#x7684;&#x3002;</p>
<pre><code class="lang-shell">tsc --traceResolution
</code></pre>
<h2 id="&#x5FEB;&#x6377;&#x5916;&#x90E8;&#x6A21;&#x5757;&#x58F0;&#x660E;">&#x5FEB;&#x6377;&#x5916;&#x90E8;&#x6A21;&#x5757;&#x58F0;&#x660E;</h2>
<p>&#x5F53;&#x4F60;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x65B0;&#x6A21;&#x5757;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x60F3;&#x8981;&#x82B1;&#x8D39;&#x65F6;&#x95F4;&#x4E66;&#x5199;&#x4E00;&#x4E2A;&#x58F0;&#x660E;&#x65F6;&#xFF0C;&#x73B0;&#x5728;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5FEB;&#x6377;&#x58F0;&#x660E;&#x4EE5;&#x4FBF;&#x4EE5;&#x5FEB;&#x901F;&#x5F00;&#x59CB;&#x3002;</p>
<h4 id="declarationsdts">declarations.d.ts</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> &quot;hot-new-<span class="hljs-keyword">module</span>&quot;;
</code></pre>
<p>&#x6240;&#x6709;&#x4ECE;&#x5FEB;&#x6377;&#x6A21;&#x5757;&#x7684;&#x5BFC;&#x5165;&#x90FD;&#x5177;&#x6709;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> x, {y} from <span class="hljs-string">&quot;hot-new-module&quot;</span>;
x(y);
</code></pre>
<h2 id="&#x6A21;&#x5757;&#x540D;&#x79F0;&#x4E2D;&#x7684;&#x901A;&#x914D;&#x7B26;">&#x6A21;&#x5757;&#x540D;&#x79F0;&#x4E2D;&#x7684;&#x901A;&#x914D;&#x7B26;</h2>
<p>&#x4EE5;&#x524D;&#x4F7F;&#x7528;&#x6A21;&#x5757;&#x52A0;&#x8F7D;&#x5668;&#xFF08;&#x4F8B;&#x5982;<a href="https://github.com/amdjs/amdjs-api/blob/master/LoaderPlugins.md" target="_blank">AMD</a>&#x548C;<a href="https://github.com/systemjs/systemjs/blob/master/docs/creating-plugins.md" target="_blank">SystemJS</a>&#xFF09;&#x5BFC;&#x5165;&#x6CA1;&#x6709;&#x4EE3;&#x7801;&#x7684;&#x8D44;&#x6E90;&#x662F;&#x4E0D;&#x5BB9;&#x6613;&#x7684;&#x3002;&#x4E4B;&#x524D;&#xFF0C;&#x5FC5;&#x987B;&#x4E3A;&#x6BCF;&#x4E2A;&#x8D44;&#x6E90;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x5916;&#x90E8;&#x6A21;&#x5757;&#x58F0;&#x660E;&#x3002;</p>
<p>TypeScript 2.0&#x652F;&#x6301;&#x4F7F;&#x7528;&#x901A;&#x914D;&#x7B26;&#x7B26;&#x53F7;&#xFF08;<code>*</code>&#xFF09;&#x5B9A;&#x4E49;&#x4E00;&#x7C7B;&#x6A21;&#x5757;&#x540D;&#x79F0;&#x3002;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x4E00;&#x4E2A;&#x58F0;&#x660E;&#x53EA;&#x9700;&#x8981;&#x4E00;&#x6B21;&#x6269;&#x5C55;&#x540D;&#xFF0C;&#x800C;&#x4E0D;&#x518D;&#x662F;&#x6BCF;&#x4E00;&#x4E2A;&#x8D44;&#x6E90;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> &quot;*!text&quot; {
    <span class="hljs-keyword">const</span> content: <span class="hljs-built_in">string</span>;
    <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> content;
}
<span class="hljs-comment">// Some do it the other way around.</span>
<span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> &quot;json!*&quot; {
    <span class="hljs-keyword">const</span> value: <span class="hljs-built_in">any</span>;
    <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> value;
}
</code></pre>
<p>&#x73B0;&#x5728;&#x4F60;&#x53EF;&#x4EE5;&#x5BFC;&#x5165;&#x5339;&#x914D;<code>&quot;*!text&quot;</code>&#x6216;<code>&quot;json!*&quot;</code>&#x7684;&#x4E1C;&#x897F;&#x4E86;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> fileContent from <span class="hljs-string">&quot;./xyz.txt!text&quot;</span>;
<span class="hljs-keyword">import</span> data from <span class="hljs-string">&quot;json!http://example.com/data.json&quot;</span>;
<span class="hljs-built_in">console</span>.log(data, fileContent);
</code></pre>
<p>&#x5F53;&#x4ECE;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E;&#x975E;&#x7C7B;&#x578B;&#x5316;&#x7684;&#x4EE3;&#x7801;&#x8FC1;&#x79FB;&#x65F6;&#xFF0C;&#x901A;&#x914D;&#x7B26;&#x6A21;&#x5757;&#x7684;&#x540D;&#x79F0;&#x53EF;&#x80FD;&#x66F4;&#x52A0;&#x6709;&#x7528;&#x3002;&#x7ED3;&#x5408;&#x5FEB;&#x6377;&#x5916;&#x90E8;&#x6A21;&#x5757;&#x58F0;&#x660E;&#xFF0C;&#x4E00;&#x7EC4;&#x6A21;&#x5757;&#x53EF;&#x4EE5;&#x5F88;&#x5BB9;&#x6613;&#x5730;&#x58F0;&#x660E;&#x4E3A;<code>any</code>&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> &quot;myLibrary/*&quot;;
</code></pre>
<p>&#x6240;&#x6709;&#x4F4D;&#x4E8E;<code>myLibrary</code>&#x76EE;&#x5F55;&#x4E4B;&#x4E0B;&#x7684;&#x6A21;&#x5757;&#x7684;&#x5BFC;&#x5165;&#x90FD;&#x88AB;&#x7F16;&#x8BD1;&#x5668;&#x8BA4;&#x4E3A;&#x662F;<code>any</code>&#x7C7B;&#x578B;&#xFF0C;&#x56E0;&#x6B64;&#x8FD9;&#x4E9B;&#x6A21;&#x5757;&#x7684;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x90FD;&#x4F1A;&#x88AB;&#x5173;&#x95ED;&#x3002;</p>
<pre><code class="lang-typescript">import { readFile } from &quot;myLibrary/fileSystem/readFile`;

readFile(); // readFile&#x662F;&apos;any&apos;&#x7C7B;&#x578B;
</code></pre>
<h2 id="&#x652F;&#x6301;umd&#x6A21;&#x5757;&#x5B9A;&#x4E49;">&#x652F;&#x6301;UMD&#x6A21;&#x5757;&#x5B9A;&#x4E49;</h2>
<p>&#x4E00;&#x4E9B;&#x5E93;&#x88AB;&#x8BBE;&#x8BA1;&#x4E3A;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x591A;&#x79CD;&#x6A21;&#x5757;&#x52A0;&#x8F7D;&#x5668;&#x6216;&#x8005;&#x4E0D;&#x662F;&#x4F7F;&#x7528;&#x6A21;&#x5757;&#x52A0;&#x8F7D;&#x5668;&#xFF08;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF09;&#x6765;&#x4F7F;&#x7528;&#xFF0C;&#x8FD9;&#x88AB;&#x79F0;&#x4E3A;<a href="https://github.com/umdjs/umd" target="_blank">UMD</a>&#x6216;<a href="http://isomorphic.net/" target="_blank">&#x540C;&#x6784;</a>&#x6A21;&#x5757;&#x3002;&#x8FD9;&#x4E9B;&#x5E93;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5BFC;&#x5165;&#x6216;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x8BBF;&#x95EE;&#x3002;</p>
<p>&#x4E3E;&#x4F8B;&#xFF1A;</p>
<h5 id="math-libdts">math-lib.d.ts</h5>
<pre><code class="lang-typescript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> isPrime(x: <span class="hljs-built_in">number</span>): <span class="hljs-built_in">boolean</span>;
<span class="hljs-keyword">export</span> as <span class="hljs-keyword">namespace</span> mathLib;
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x8BE5;&#x5E93;&#x53EF;&#x4F5C;&#x4E3A;&#x6A21;&#x5757;&#x5BFC;&#x5165;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> { isPrime } from <span class="hljs-string">&quot;math-lib&quot;</span>;
isPrime(<span class="hljs-number">2</span>);
mathLib.isPrime(<span class="hljs-number">2</span>); <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF1A;&#x65E0;&#x6CD5;&#x5728;&#x6A21;&#x5757;&#x5185;&#x90E8;&#x4F7F;&#x7528;&#x5168;&#x5C40;&#x5B9A;&#x4E49;</span>
</code></pre>
<p>&#x5B83;&#x4E5F;&#x53EF;&#x4EE5;&#x88AB;&#x7528;&#x6765;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x53EA;&#x9650;&#x4E8E;&#x6CA1;&#x6709;<code>import</code>&#x548C;<code>export</code>&#x811A;&#x672C;&#x6587;&#x4EF6;&#x4E2D;&#x3002;</p>
<pre><code class="lang-typescript">mathLib.isPrime(<span class="hljs-number">2</span>);
</code></pre>
<h2 id="&#x53EF;&#x9009;&#x7C7B;&#x5C5E;&#x6027;">&#x53EF;&#x9009;&#x7C7B;&#x5C5E;&#x6027;</h2>
<p>&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x5728;&#x7C7B;&#x4E2D;&#x58F0;&#x660E;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0E;&#x63A5;&#x53E3;&#x7C7B;&#x4F3C;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Bar {
    a: <span class="hljs-built_in">number</span>;
    b?: <span class="hljs-built_in">number</span>;
    f() {
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    }
    g?(): <span class="hljs-built_in">number</span>;  <span class="hljs-comment">// &#x53EF;&#x9009;&#x65B9;&#x6CD5;&#x7684;&#x65B9;&#x6CD5;&#x4F53;&#x53EF;&#x4EE5;&#x7701;&#x7565;</span>
    h?() {
        <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>;
    }
}
</code></pre>
<p>&#x5728;<code>--strictNullChecks</code>&#x6A21;&#x5F0F;&#x4E0B;&#x7F16;&#x8BD1;&#x65F6;&#xFF0C;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x4F1A;&#x81EA;&#x52A8;&#x6DFB;&#x52A0;<code>undefined</code>&#x5230;&#x5B83;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x4E2D;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x4E0A;&#x9762;&#x7684;<code>b</code>&#x5C5E;&#x6027;&#x7C7B;&#x578B;&#x662F;<code>number | undefined</code>&#xFF0C;&#x4E0A;&#x9762;<code>g</code>&#x65B9;&#x6CD5;&#x7684;&#x7C7B;&#x578B;&#x662F;<code>(()=&gt; number) | undefined</code>&#x3002;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x4FDD;&#x62A4;&#x53EF;&#x4EE5;&#x53BB;&#x9664;<code>undefined</code>&#x3002;</p>
<h2 id="&#x79C1;&#x6709;&#x7684;&#x548C;&#x53D7;&#x4FDD;&#x62A4;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;">&#x79C1;&#x6709;&#x7684;&#x548C;&#x53D7;&#x4FDD;&#x62A4;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;</h2>
<p>&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x88AB;&#x6807;&#x8BB0;&#x4E3A;<code>private</code>&#x6216;<code>protected</code>&#x3002;&#x79C1;&#x6709;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x7C7B;&#x4E0D;&#x80FD;&#x5728;&#x7C7B;&#x7684;&#x5916;&#x90E8;&#x5B9E;&#x4F8B;&#x5316;&#xFF0C;&#x5E76;&#x4E14;&#x4E5F;&#x4E0D;&#x80FD;&#x88AB;&#x7EE7;&#x627F;&#x3002;&#x53D7;&#x4FDD;&#x62A4;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x7C7B;&#x4E0D;&#x80FD;&#x518D;&#x7C7B;&#x7684;&#x5916;&#x90E8;&#x5B9E;&#x4F8B;&#x5316;&#xFF0C;&#x4F46;&#x662F;&#x53EF;&#x4EE5;&#x88AB;&#x7EE7;&#x627F;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Singleton {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> instance: Singleton;

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">constructor</span>() { }

    <span class="hljs-keyword">static</span> getInstance() {
        <span class="hljs-keyword">if</span> (!Singleton.instance) {
            Singleton.instance = <span class="hljs-keyword">new</span> Singleton();
        }
        <span class="hljs-keyword">return</span> Singleton.instance;
    }
}

<span class="hljs-keyword">let</span> e = <span class="hljs-keyword">new</span> Singleton(); <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF1A;Singleton&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x662F;&#x79C1;&#x6709;&#x7684;&#x3002;</span>
<span class="hljs-keyword">let</span> v = Singleton.getInstance();
</code></pre>
<h2 id="&#x62BD;&#x8C61;&#x5C5E;&#x6027;&#x548C;&#x8BBF;&#x95EE;&#x5668;">&#x62BD;&#x8C61;&#x5C5E;&#x6027;&#x548C;&#x8BBF;&#x95EE;&#x5668;</h2>
<p>&#x62BD;&#x8C61;&#x7C7B;&#x53EF;&#x4EE5;&#x58F0;&#x660E;&#x62BD;&#x8C61;&#x5C5E;&#x6027;&#x548C;&#x3001;&#x6216;&#x8BBF;&#x95EE;&#x5668;&#x3002;&#x6240;&#x6709;&#x5B50;&#x7C7B;&#x5C06;&#x9700;&#x8981;&#x58F0;&#x660E;&#x62BD;&#x8C61;&#x5C5E;&#x6027;&#x6216;&#x8005;&#x88AB;&#x6807;&#x8BB0;&#x4E3A;&#x62BD;&#x8C61;&#x7684;&#x3002;&#x62BD;&#x8C61;&#x5C5E;&#x6027;&#x4E0D;&#x80FD;&#x521D;&#x59CB;&#x5316;&#x3002;&#x62BD;&#x8C61;&#x8BBF;&#x95EE;&#x5668;&#x4E0D;&#x80FD;&#x6709;&#x5177;&#x4F53;&#x4EE3;&#x7801;&#x5757;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> Base {
    <span class="hljs-keyword">abstract</span> name: <span class="hljs-built_in">string</span>;
    <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">get</span> value();
    <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">set</span> value(v: <span class="hljs-built_in">number</span>);
}

<span class="hljs-keyword">class</span> Derived extends Base {
    name = <span class="hljs-string">&quot;derived&quot;</span>;

    value = <span class="hljs-number">1</span>;
}
</code></pre>
<h2 id="&#x9690;&#x5F0F;&#x7D22;&#x5F15;&#x7B7E;&#x540D;">&#x9690;&#x5F0F;&#x7D22;&#x5F15;&#x7B7E;&#x540D;</h2>
<p>&#x5982;&#x679C;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x4E2D;&#x6240;&#x6709;&#x5DF2;&#x77E5;&#x7684;&#x5C5E;&#x6027;&#x662F;&#x8D4B;&#x503C;&#x7ED9;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#xFF0C;&#x90A3;&#x4E48;&#x73B0;&#x5728;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x7C7B;&#x578B;&#x3002;&#x8FD9;&#x4F7F;&#x5F97;&#x4E00;&#x4E2A;&#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x521D;&#x59CB;&#x5316;&#x7684;&#x53D8;&#x91CF;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x671F;&#x671B;&#x53C2;&#x6570;&#x662F;map&#x6216;dictionary&#x7684;&#x51FD;&#x6570;&#x6210;&#x4E3A;&#x53EF;&#x80FD;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">httpService</span>(<span class="hljs-params">path: <span class="hljs-built_in">string</span>, headers: { [x: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">string</span> }</span>) </span>{ }

<span class="hljs-keyword">const</span> headers = {
    <span class="hljs-string">&quot;Content-Type&quot;</span>: <span class="hljs-string">&quot;application/x-www-form-urlencoded&quot;</span>
};

httpService(<span class="hljs-string">&quot;&quot;</span>, { <span class="hljs-string">&quot;Content-Type&quot;</span>: <span class="hljs-string">&quot;application/x-www-form-urlencoded&quot;</span> });  <span class="hljs-comment">// &#x53EF;&#x4EE5;</span>
httpService(<span class="hljs-string">&quot;&quot;</span>, headers);  <span class="hljs-comment">// &#x73B0;&#x5728;&#x53EF;&#x4EE5;&#xFF0C;&#x4EE5;&#x524D;&#x4E0D;&#x53EF;&#x4EE5;&#x3002;</span>
</code></pre>
<h2 id="&#x4F7F;&#x7528;--lib&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#x5305;&#x542B;&#x5185;&#x7F6E;&#x7C7B;&#x578B;&#x58F0;&#x660E;">&#x4F7F;&#x7528;<code>--lib</code>&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#x5305;&#x542B;&#x5185;&#x7F6E;&#x7C7B;&#x578B;&#x58F0;&#x660E;</h2>
<p>&#x83B7;&#x53D6;ES6/ES2015&#x5185;&#x7F6E;API&#x58F0;&#x660E;&#x4EC5;&#x9650;&#x4E8E;<code>target: ES6</code>&#x3002;&#x8F93;&#x5165;<code>--lib</code>&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>--lib</code>&#x6307;&#x5B9A;&#x4E00;&#x7EC4;&#x9879;&#x76EE;&#x6240;&#x9700;&#x8981;&#x7684;&#x5185;&#x7F6E;API&#x3002;&#x6BD4;&#x5982;&#x8BF4;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x5E0C;&#x671B;&#x9879;&#x76EE;&#x8FD0;&#x884C;&#x65F6;&#x652F;&#x6301;<code>Map</code>&#x3001;<code>Set</code>&#x548C;<code>Promise</code>&#xFF08;&#x4F8B;&#x5982;&#x73B0;&#x5728;&#x9759;&#x9ED8;&#x66F4;&#x65B0;&#x6D4F;&#x89C8;&#x5668;&#xFF09;&#xFF0C;&#x76F4;&#x63A5;&#x5199;<code>--lib es2015.collection,es2015.promise</code>&#x5C31;&#x597D;&#x4E86;&#x3002;&#x540C;&#x6837;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x6392;&#x9664;&#x9879;&#x76EE;&#x4E2D;&#x4E0D;&#x9700;&#x8981;&#x7684;&#x58F0;&#x660E;&#xFF0C;&#x4F8B;&#x5982;&#x5728;node&#x9879;&#x76EE;&#x4E2D;&#x4F7F;&#x7528;<code>--lib es5,es6</code>&#x6392;&#x9664;DOM&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x5217;&#x51FA;&#x4E86;&#x53EF;&#x7528;&#x7684;API&#xFF1A;</p>
<ul>
<li>dom</li>
<li>webworker</li>
<li>es5</li>
<li>es6 / es2015</li>
<li>es2015.core</li>
<li>es2015.collection</li>
<li>es2015.iterable</li>
<li>es2015.promise</li>
<li>es2015.proxy</li>
<li>es2015.reflect</li>
<li>es2015.generator</li>
<li>es2015.symbol</li>
<li>es2015.symbol.wellknown</li>
<li>es2016</li>
<li>es2016.array.include</li>
<li>es2017</li>
<li>es2017.object</li>
<li>es2017.sharedmemory</li>
<li>scripthost</li>
</ul>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-shell">tsc --target es5 --lib es5,es2015.promise
</code></pre>
<pre><code class="lang-json"><span class="hljs-string">&quot;compilerOptions&quot;</span>: {
    <span class="hljs-string">&quot;lib&quot;</span>: [<span class="hljs-string">&quot;es5&quot;</span>, <span class="hljs-string">&quot;es2015.promise&quot;</span>]
}
</code></pre>
<h2 id="&#x4F7F;&#x7528;--nounusedparameters&#x548C;--nounusedlocals&#x6807;&#x8BB0;&#x672A;&#x4F7F;&#x7528;&#x7684;&#x58F0;&#x660E;">&#x4F7F;&#x7528;<code>--noUnusedParameters</code>&#x548C;<code>--noUnusedLocals</code>&#x6807;&#x8BB0;&#x672A;&#x4F7F;&#x7528;&#x7684;&#x58F0;&#x660E;</h2>
<p>TypeScript 2.0&#x6709;&#x4E24;&#x4E2A;&#x65B0;&#x7684;&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#x6765;&#x5E2E;&#x52A9;&#x4F60;&#x4FDD;&#x6301;&#x4E00;&#x4E2A;&#x5E72;&#x51C0;&#x7684;&#x4EE3;&#x7801;&#x5E93;&#x3002;<code>-noUnusedParameters</code>&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#x6807;&#x8BB0;&#x6240;&#x6709;&#x672A;&#x4F7F;&#x7528;&#x7684;&#x51FD;&#x6570;&#x6216;&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x9519;&#x8BEF;&#x3002;<code>--noUnusedLocals</code>&#x6807;&#x8BB0;&#x6240;&#x6709;&#x672A;&#x4F7F;&#x7528;&#x7684;&#x5C40;&#x90E8;&#xFF08;&#x672A;&#x5BFC;&#x51FA;&#xFF09;&#x58F0;&#x660E;&#x50CF;&#x53D8;&#x91CF;&#x3001;&#x51FD;&#x6570;&#x3001;&#x7C7B;&#x548C;&#x5BFC;&#x5165;&#x7B49;&#x7B49;&#xFF0C;&#x53E6;&#x5916;&#x672A;&#x4F7F;&#x7528;&#x7684;&#x79C1;&#x6709;&#x7C7B;&#x6210;&#x5458;&#x5728;<code>--noUnusedLocals</code>&#x4F5C;&#x7528;&#x4E0B;&#x4E5F;&#x4F1A;&#x6807;&#x8BB0;&#x4E3A;&#x9519;&#x8BEF;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> B, { readFile } from <span class="hljs-string">&quot;./b&quot;</span>;
<span class="hljs-comment">//     ^ &#x9519;&#x8BEF;&#xFF1A;`B`&#x58F0;&#x660E;&#x4E86;&#xFF0C;&#x4F46;&#x662F;&#x6CA1;&#x6709;&#x4F7F;&#x7528;&#x3002;</span>
readFile();


<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">write</span>(<span class="hljs-params">message: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">string</span>[]</span>) </span>{
    <span class="hljs-comment">//                                 ^^^^  &#x9519;&#x8BEF;&#xFF1A;&apos;arg&apos;&#x58F0;&#x660E;&#x4E86;&#xFF0C;&#x4F46;&#x662F;&#x6CA1;&#x6709;&#x4F7F;&#x7528;&#x3002;</span>
    <span class="hljs-built_in">console</span>.log(message);
}
</code></pre>
<p>&#x4F7F;&#x7528;&#x4EE5;<code>_</code>&#x5F00;&#x5934;&#x547D;&#x540D;&#x7684;&#x53C2;&#x6570;&#x58F0;&#x660E;&#x4E0D;&#x4F1A;&#x88AB;&#x672A;&#x4F7F;&#x7528;&#x53C2;&#x6570;&#x68C0;&#x67E5;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">returnNull</span>(<span class="hljs-params">_a</span>) </span>{ <span class="hljs-comment">// &#x6B63;&#x786E;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
}
</code></pre>
<h2 id="&#x6A21;&#x5757;&#x540D;&#x79F0;&#x5141;&#x8BB8;js&#x6269;&#x5C55;&#x540D;">&#x6A21;&#x5757;&#x540D;&#x79F0;&#x5141;&#x8BB8;<code>.js</code>&#x6269;&#x5C55;&#x540D;</h2>
<p>TypeScript 2.0&#x4E4B;&#x524D;&#xFF0C;&#x6A21;&#x5757;&#x540D;&#x79F0;&#x603B;&#x662F;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x6CA1;&#x6709;&#x6269;&#x5C55;&#x540D;&#x7684;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5BFC;&#x5165;&#x4E00;&#x4E2A;&#x6A21;&#x5757;<code>import d from &quot;./moduleA.js&quot;</code>&#xFF0C;&#x5219;&#x7F16;&#x8BD1;&#x5668;&#x5728;<code>./moduleA.js.ts</code>&#x6216;<code>./moduleA.js.d.ts</code>&#x4E2D;&#x67E5;&#x627E;<code>&quot;moduleA.js&quot;</code>&#x7684;&#x5B9A;&#x4E49;&#x3002;&#x8FD9;&#x4F7F;&#x5F97;&#x50CF;<a href="https://github.com/systemjs/systemjs" target="_blank">SystemJS</a>&#x8FD9;&#x79CD;&#x671F;&#x671B;&#x6A21;&#x5757;&#x540D;&#x79F0;&#x662F;URI&#x7684;&#x6253;&#x5305;&#x6216;&#x52A0;&#x8F7D;&#x5DE5;&#x5177;&#x5F88;&#x96BE;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x4F7F;&#x7528;TypeScript 2.0&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x5C06;&#x5728;<code>./moduleA.ts</code>&#x6216;<code>./moduleA.d.ts</code>&#x4E2D;&#x67E5;&#x627E;<code>&quot;moduleA.js&quot;</code>&#x7684;&#x5B9A;&#x4E49;&#x3002;</p>
<h2 id="&#x652F;&#x6301;&#x7F16;&#x8BD1;&#x53C2;&#x6570;target--es5&#x548C;module-es6&#x540C;&#x65F6;&#x4F7F;&#x7528;">&#x652F;&#x6301;&#x7F16;&#x8BD1;&#x53C2;&#x6570;<code>target : es5</code>&#x548C;<code>module: es6</code>&#x540C;&#x65F6;&#x4F7F;&#x7528;</h2>
<p>&#x4E4B;&#x524D;&#x7F16;&#x8BD1;&#x53C2;&#x6570;<code>target : es5</code>&#x548C;<code>module: es6</code>&#x540C;&#x65F6;&#x4F7F;&#x7528;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x65E0;&#x6548;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x73B0;&#x5728;&#x662F;&#x6709;&#x6548;&#x7684;&#x3002;&#x8FD9;&#x5C06;&#x6709;&#x52A9;&#x4E8E;&#x4F7F;&#x7528;&#x57FA;&#x4E8E;ES2015&#x7684;tree-shaking&#xFF08;&#x5C06;&#x65E0;&#x7528;&#x4EE3;&#x7801;&#x79FB;&#x9664;&#xFF09;&#x6BD4;&#x5982;<a href="https://github.com/rollup/rollup" target="_blank">rollup</a>&#x3002;</p>
<h2 id="&#x51FD;&#x6570;&#x5F62;&#x53C2;&#x548C;&#x5B9E;&#x53C2;&#x5217;&#x8868;&#x672B;&#x5C3E;&#x652F;&#x6301;&#x9017;&#x53F7;">&#x51FD;&#x6570;&#x5F62;&#x53C2;&#x548C;&#x5B9E;&#x53C2;&#x5217;&#x8868;&#x672B;&#x5C3E;&#x652F;&#x6301;&#x9017;&#x53F7;</h2>
<p>&#x73B0;&#x5728;&#x51FD;&#x6570;&#x5F62;&#x53C2;&#x548C;&#x5B9E;&#x53C2;&#x5217;&#x8868;&#x672B;&#x5C3E;&#x5141;&#x8BB8;&#x6709;&#x9017;&#x53F7;&#x3002;&#x8FD9;&#x662F;&#x5BF9;<a href="https://jeffmo.github.io/es-trailing-function-commas/" target="_blank">&#x7B2C;&#x4E09;&#x9636;&#x6BB5;&#x7684;ECMAScript&#x63D0;&#x6848;</a>&#x7684;&#x5B9E;&#x73B0;, &#x5E76;&#x4E14;&#x4F1A;&#x7F16;&#x8BD1;&#x4E3A;&#x53EF;&#x7528;&#x7684; ES3/ES5/ES6&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">
  bar: Bar,
  baz: Baz, <span class="hljs-comment">// &#x5F62;&#x53C2;&#x5217;&#x8868;&#x672B;&#x5C3E;&#x6DFB;&#x52A0;&#x9017;&#x53F7;&#x662F;&#x6CA1;&#x6709;&#x95EE;&#x9898;&#x7684;&#x3002;</span>
</span>) </span>{
  <span class="hljs-comment">// &#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x2026;&#x2026;</span>
}

foo(
  bar,
  baz, <span class="hljs-comment">// &#x5B9E;&#x53C2;&#x5217;&#x8868;&#x672B;&#x5C3E;&#x6DFB;&#x52A0;&#x9017;&#x53F7;&#x540C;&#x6837;&#x6CA1;&#x6709;&#x95EE;&#x9898;</span>
);
</code></pre>
<h2 id="&#x65B0;&#x7F16;&#x8BD1;&#x53C2;&#x6570;--skiplibcheck">&#x65B0;&#x7F16;&#x8BD1;&#x53C2;&#x6570;<code>--skipLibCheck</code></h2>
<p>TypeScript 2.0&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7F16;&#x8BD1;&#x53C2;&#x6570;<code>--skipLibCheck</code>&#xFF0C;&#x8BE5;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x8DF3;&#x8FC7;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#xFF08;&#x4EE5;<code>.d.ts</code>&#x4E3A;&#x6269;&#x5C55;&#x540D;&#x7684;&#x6587;&#x4EF6;&#xFF09;&#x7684;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#x3002;&#x5F53;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;&#x5305;&#x542B;&#x6709;&#x5927;&#x91CF;&#x7684;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x65F6;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x9700;&#x8981;&#x82B1;&#x8D39;&#x5927;&#x91CF;&#x65F6;&#x95F4;&#x5BF9;&#x5DF2;&#x77E5;&#x4E0D;&#x5305;&#x542B;&#x9519;&#x8BEF;&#x7684;&#x58F0;&#x660E;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#xFF0C;&#x901A;&#x8FC7;&#x8DF3;&#x8FC7;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x7684;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#xFF0C;&#x7F16;&#x8BD1;&#x65F6;&#x95F4;&#x53EF;&#x80FD;&#x4F1A;&#x5927;&#x5927;&#x7F29;&#x77ED;&#x3002;</p>
<p>&#x7531;&#x4E8E;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x58F0;&#x660E;&#x53EF;&#x4EE5;&#x5F71;&#x54CD;&#x5176;&#x4ED6;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x7C7B;&#x578B;&#x68C0;&#x67E5;&#xFF0C;&#x5F53;&#x6307;&#x5B9A;<code>--skipLibCheck</code>&#x65F6;&#xFF0C;&#x4E00;&#x4E9B;&#x9519;&#x8BEF;&#x53EF;&#x80FD;&#x68C0;&#x6D4B;&#x4E0D;&#x5230;&#x3002;&#x6BD4;&#x5982;&#x8BF4;, &#x5982;&#x679C;&#x4E00;&#x4E2A;&#x975E;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x7C7B;&#x578B;&#x88AB;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x7528;&#x5230;, &#x53EF;&#x80FD;&#x4EC5;&#x5728;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x88AB;&#x68C0;&#x67E5;&#x65F6;&#x80FD;&#x53D1;&#x73B0;&#x9519;&#x8BEF;. &#x4E0D;&#x8FC7;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x5728;&#x5B9E;&#x9645;&#x4F7F;&#x7528;&#x4E2D;&#x5E76;&#x4E0D;&#x5E38;&#x89C1;&#x3002;</p>
<h2 id="&#x5141;&#x8BB8;&#x5728;&#x58F0;&#x660E;&#x4E2D;&#x91CD;&#x590D;&#x6807;&#x8BC6;&#x7B26;">&#x5141;&#x8BB8;&#x5728;&#x58F0;&#x660E;&#x4E2D;&#x91CD;&#x590D;&#x6807;&#x8BC6;&#x7B26;</h2>
<p>&#x8FD9;&#x662F;&#x91CD;&#x590D;&#x5B9A;&#x4E49;&#x9519;&#x8BEF;&#x7684;&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x6765;&#x6E90;&#x3002;&#x591A;&#x4E2A;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x5B9A;&#x4E49;&#x76F8;&#x540C;&#x7684;&#x63A5;&#x53E3;&#x6210;&#x5458;&#x3002;</p>
<p>TypeScript 2.0&#x653E;&#x5BBD;&#x4E86;&#x8FD9;&#x4E00;&#x7EA6;&#x675F;&#xFF0C;&#x5E76;&#x5141;&#x8BB8;&#x53EF;&#x4EE5;&#x4E0D;&#x540C;&#x4EE3;&#x7801;&#x5757;&#x4E2D;&#x51FA;&#x73B0;&#x91CD;&#x590D;&#x7684;&#x6807;&#x8BC6;&#x7B26;, &#x53EA;&#x8981;&#x5B83;&#x4EEC;&#x6709;<em>&#x5B8C;&#x5168;&#x76F8;&#x540C;</em>&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x5728;&#x540C;&#x4E00;&#x4EE3;&#x7801;&#x5757;&#x91CD;&#x590D;&#x5B9A;&#x4E49;&#x4ECD;&#x4E0D;&#x5141;&#x8BB8;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">interface</span> Error {
    stack?: <span class="hljs-built_in">string</span>;
}


<span class="hljs-keyword">interface</span> Error {
    code?: <span class="hljs-built_in">string</span>;
    path?: <span class="hljs-built_in">string</span>;
    stack?: <span class="hljs-built_in">string</span>;  <span class="hljs-comment">// OK</span>
}
</code></pre>
<h2 id="&#x65B0;&#x7F16;&#x8BD1;&#x53C2;&#x6570;--declarationdir">&#x65B0;&#x7F16;&#x8BD1;&#x53C2;&#x6570;<code>--declarationDir</code></h2>
<p><code>--declarationDir</code>&#x53EF;&#x4EE5;&#x4F7F;&#x751F;&#x6210;&#x7684;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x548C;JavaScript&#x6587;&#x4EF6;&#x4E0D;&#x5728;&#x540C;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;&#x4E2D;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="TypeScript 2.1.html" class="navigation navigation-prev " aria-label="Previous page: TypeScript 2.1">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="TypeScript 1.8.html" class="navigation navigation-next " aria-label="Next page: TypeScript 1.8">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"TypeScript 2.0","level":"1.7.12","depth":2,"next":{"title":"TypeScript 1.8","level":"1.7.13","depth":2,"path":"doc/release-notes/TypeScript 1.8.md","ref":"./doc/release-notes/TypeScript 1.8.md","articles":[]},"previous":{"title":"TypeScript 2.1","level":"1.7.11","depth":2,"path":"doc/release-notes/TypeScript 2.1.md","ref":"./doc/release-notes/TypeScript 2.1.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Patrick Zhong","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"preface.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"TypeScript Handbook","language":"zh-hans","gitbook":">3.0.0","description":"Translation of official TypeScript Handbook"},"file":{"path":"doc/release-notes/TypeScript 2.0.md","mtime":"2018-10-13T09:48:54.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-13T09:57:42.904Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

